blob: bfff1d1c70ab0149b79bea42752140e292a9fe19 [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>
Joe Howse0f5b2652014-12-29 12:00:03 -030015 * PS3 Eye camera - FPS range extended by Joseph Howse
16 * <josephhowse@nummist.com> http://nummist.com
Jean-Francois Moine189d92a2009-11-11 07:46:28 -030017 *
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030018 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 */
32
Joe Perches133a9fe2011-08-21 19:56:57 -030033#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030035#define MODULE_NAME "ov534"
36
37#include "gspca.h"
38
Antonio Ospitee89fca92012-05-14 08:07:45 -030039#include <linux/fixp-arith.h>
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -030040#include <media/v4l2-ctrls.h>
Antonio Ospitee89fca92012-05-14 08:07:45 -030041
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -030042#define OV534_REG_ADDRESS 0xf1 /* sensor address */
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030043#define OV534_REG_SUBADDR 0xf2
44#define OV534_REG_WRITE 0xf3
45#define OV534_REG_READ 0xf4
46#define OV534_REG_OPERATION 0xf5
47#define OV534_REG_STATUS 0xf6
48
49#define OV534_OP_WRITE_3 0x37
50#define OV534_OP_WRITE_2 0x33
51#define OV534_OP_READ_2 0xf9
52
53#define CTRL_TIMEOUT 500
54
55MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
56MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
57MODULE_LICENSE("GPL");
58
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030059/* specific webcam descriptor */
60struct sd {
61 struct gspca_dev gspca_dev; /* !! must be the first item */
Jean-François Moine228dd262011-02-10 09:34:57 -030062
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -030063 struct v4l2_ctrl_handler ctrl_handler;
64 struct v4l2_ctrl *hue;
65 struct v4l2_ctrl *saturation;
66 struct v4l2_ctrl *brightness;
67 struct v4l2_ctrl *contrast;
68 struct { /* gain control cluster */
69 struct v4l2_ctrl *autogain;
70 struct v4l2_ctrl *gain;
71 };
72 struct v4l2_ctrl *autowhitebalance;
73 struct { /* exposure control cluster */
74 struct v4l2_ctrl *autoexposure;
75 struct v4l2_ctrl *exposure;
76 };
77 struct v4l2_ctrl *sharpness;
78 struct v4l2_ctrl *hflip;
79 struct v4l2_ctrl *vflip;
80 struct v4l2_ctrl *plfreq;
Jean-François Moine228dd262011-02-10 09:34:57 -030081
Jean-Francois Moine8c252052008-12-04 05:06:08 -030082 __u32 last_pts;
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -030083 u16 last_fid;
84 u8 frame_rate;
Jean-Francois Moineb014f942009-11-11 14:28:53 -030085
Jean-François Moine458efe22011-02-10 10:11:04 -030086 u8 sensor;
87};
88enum sensors {
89 SENSOR_OV767x,
90 SENSOR_OV772x,
91 NSENSORS
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030092};
93
Jean-François Moine228dd262011-02-10 09:34:57 -030094static int sd_start(struct gspca_dev *gspca_dev);
95static void sd_stopN(struct gspca_dev *gspca_dev);
Jean-Francois Moine189d92a2009-11-11 07:46:28 -030096
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030097
Jean-Francois Moine569691a2009-11-14 09:45:38 -030098static const struct v4l2_pix_format ov772x_mode[] = {
Jean-Francois Moine189d92a2009-11-11 07:46:28 -030099 {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
100 .bytesperline = 320 * 2,
101 .sizeimage = 320 * 240 * 2,
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300102 .colorspace = V4L2_COLORSPACE_SRGB,
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300103 .priv = 1},
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300104 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
105 .bytesperline = 640 * 2,
106 .sizeimage = 640 * 480 * 2,
Jean-Francois Moine191d0e72009-05-22 04:16:42 -0300107 .colorspace = V4L2_COLORSPACE_SRGB,
108 .priv = 0},
109};
Jean-François Moine458efe22011-02-10 10:11:04 -0300110static const struct v4l2_pix_format ov767x_mode[] = {
111 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
112 .bytesperline = 320,
113 .sizeimage = 320 * 240 * 3 / 8 + 590,
114 .colorspace = V4L2_COLORSPACE_JPEG},
115 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
116 .bytesperline = 640,
117 .sizeimage = 640 * 480 * 3 / 8 + 590,
118 .colorspace = V4L2_COLORSPACE_JPEG},
119};
Jean-Francois Moine191d0e72009-05-22 04:16:42 -0300120
Joe Howse0f5b2652014-12-29 12:00:03 -0300121static const u8 qvga_rates[] = {187, 150, 137, 125, 100, 75, 60, 50, 37, 30};
Antonio Ospite29b87f02010-01-17 03:27:04 -0300122static const u8 vga_rates[] = {60, 50, 40, 30, 15};
123
124static const struct framerates ov772x_framerates[] = {
125 { /* 320x240 */
126 .rates = qvga_rates,
127 .nrates = ARRAY_SIZE(qvga_rates),
128 },
129 { /* 640x480 */
130 .rates = vga_rates,
131 .nrates = ARRAY_SIZE(vga_rates),
132 },
133};
134
Jean-François Moine458efe22011-02-10 10:11:04 -0300135struct reg_array {
136 const u8 (*val)[2];
137 int len;
138};
139
140static const u8 bridge_init_767x[][2] = {
141/* comments from the ms-win file apollo7670.set */
142/* str1 */
143 {0xf1, 0x42},
144 {0x88, 0xf8},
145 {0x89, 0xff},
146 {0x76, 0x03},
147 {0x92, 0x03},
148 {0x95, 0x10},
149 {0xe2, 0x00},
150 {0xe7, 0x3e},
151 {0x8d, 0x1c},
152 {0x8e, 0x00},
153 {0x8f, 0x00},
154 {0x1f, 0x00},
155 {0xc3, 0xf9},
156 {0x89, 0xff},
157 {0x88, 0xf8},
158 {0x76, 0x03},
159 {0x92, 0x01},
160 {0x93, 0x18},
161 {0x1c, 0x00},
162 {0x1d, 0x48},
163 {0x1d, 0x00},
164 {0x1d, 0xff},
165 {0x1d, 0x02},
166 {0x1d, 0x58},
167 {0x1d, 0x00},
168 {0x1c, 0x0a},
169 {0x1d, 0x0a},
170 {0x1d, 0x0e},
171 {0xc0, 0x50}, /* HSize 640 */
172 {0xc1, 0x3c}, /* VSize 480 */
173 {0x34, 0x05}, /* enable Audio Suspend mode */
174 {0xc2, 0x0c}, /* Input YUV */
175 {0xc3, 0xf9}, /* enable PRE */
176 {0x34, 0x05}, /* enable Audio Suspend mode */
177 {0xe7, 0x2e}, /* this solves failure of "SuspendResumeTest" */
178 {0x31, 0xf9}, /* enable 1.8V Suspend */
179 {0x35, 0x02}, /* turn on JPEG */
180 {0xd9, 0x10},
181 {0x25, 0x42}, /* GPIO[8]:Input */
182 {0x94, 0x11}, /* If the default setting is loaded when
183 * system boots up, this flag is closed here */
184};
185static const u8 sensor_init_767x[][2] = {
186 {0x12, 0x80},
187 {0x11, 0x03},
188 {0x3a, 0x04},
189 {0x12, 0x00},
190 {0x17, 0x13},
191 {0x18, 0x01},
192 {0x32, 0xb6},
193 {0x19, 0x02},
194 {0x1a, 0x7a},
195 {0x03, 0x0a},
196 {0x0c, 0x00},
197 {0x3e, 0x00},
198 {0x70, 0x3a},
199 {0x71, 0x35},
200 {0x72, 0x11},
201 {0x73, 0xf0},
202 {0xa2, 0x02},
203 {0x7a, 0x2a}, /* set Gamma=1.6 below */
204 {0x7b, 0x12},
205 {0x7c, 0x1d},
206 {0x7d, 0x2d},
207 {0x7e, 0x45},
208 {0x7f, 0x50},
209 {0x80, 0x59},
210 {0x81, 0x62},
211 {0x82, 0x6b},
212 {0x83, 0x73},
213 {0x84, 0x7b},
214 {0x85, 0x8a},
215 {0x86, 0x98},
216 {0x87, 0xb2},
217 {0x88, 0xca},
218 {0x89, 0xe0},
219 {0x13, 0xe0},
220 {0x00, 0x00},
221 {0x10, 0x00},
222 {0x0d, 0x40},
223 {0x14, 0x38}, /* gain max 16x */
224 {0xa5, 0x05},
225 {0xab, 0x07},
226 {0x24, 0x95},
227 {0x25, 0x33},
228 {0x26, 0xe3},
229 {0x9f, 0x78},
230 {0xa0, 0x68},
231 {0xa1, 0x03},
232 {0xa6, 0xd8},
233 {0xa7, 0xd8},
234 {0xa8, 0xf0},
235 {0xa9, 0x90},
236 {0xaa, 0x94},
237 {0x13, 0xe5},
238 {0x0e, 0x61},
239 {0x0f, 0x4b},
240 {0x16, 0x02},
241 {0x21, 0x02},
242 {0x22, 0x91},
243 {0x29, 0x07},
244 {0x33, 0x0b},
245 {0x35, 0x0b},
246 {0x37, 0x1d},
247 {0x38, 0x71},
248 {0x39, 0x2a},
249 {0x3c, 0x78},
250 {0x4d, 0x40},
251 {0x4e, 0x20},
252 {0x69, 0x00},
253 {0x6b, 0x4a},
254 {0x74, 0x10},
255 {0x8d, 0x4f},
256 {0x8e, 0x00},
257 {0x8f, 0x00},
258 {0x90, 0x00},
259 {0x91, 0x00},
260 {0x96, 0x00},
261 {0x9a, 0x80},
262 {0xb0, 0x84},
263 {0xb1, 0x0c},
264 {0xb2, 0x0e},
265 {0xb3, 0x82},
266 {0xb8, 0x0a},
267 {0x43, 0x0a},
268 {0x44, 0xf0},
269 {0x45, 0x34},
270 {0x46, 0x58},
271 {0x47, 0x28},
272 {0x48, 0x3a},
273 {0x59, 0x88},
274 {0x5a, 0x88},
275 {0x5b, 0x44},
276 {0x5c, 0x67},
277 {0x5d, 0x49},
278 {0x5e, 0x0e},
279 {0x6c, 0x0a},
280 {0x6d, 0x55},
281 {0x6e, 0x11},
282 {0x6f, 0x9f},
283 {0x6a, 0x40},
284 {0x01, 0x40},
285 {0x02, 0x40},
286 {0x13, 0xe7},
287 {0x4f, 0x80},
288 {0x50, 0x80},
289 {0x51, 0x00},
290 {0x52, 0x22},
291 {0x53, 0x5e},
292 {0x54, 0x80},
293 {0x58, 0x9e},
294 {0x41, 0x08},
295 {0x3f, 0x00},
296 {0x75, 0x04},
297 {0x76, 0xe1},
298 {0x4c, 0x00},
299 {0x77, 0x01},
300 {0x3d, 0xc2},
301 {0x4b, 0x09},
302 {0xc9, 0x60},
303 {0x41, 0x38}, /* jfm: auto sharpness + auto de-noise */
304 {0x56, 0x40},
305 {0x34, 0x11},
306 {0x3b, 0xc2},
307 {0xa4, 0x8a}, /* Night mode trigger point */
308 {0x96, 0x00},
309 {0x97, 0x30},
310 {0x98, 0x20},
311 {0x99, 0x20},
312 {0x9a, 0x84},
313 {0x9b, 0x29},
314 {0x9c, 0x03},
315 {0x9d, 0x4c},
316 {0x9e, 0x3f},
317 {0x78, 0x04},
318 {0x79, 0x01},
319 {0xc8, 0xf0},
320 {0x79, 0x0f},
321 {0xc8, 0x00},
322 {0x79, 0x10},
323 {0xc8, 0x7e},
324 {0x79, 0x0a},
325 {0xc8, 0x80},
326 {0x79, 0x0b},
327 {0xc8, 0x01},
328 {0x79, 0x0c},
329 {0xc8, 0x0f},
330 {0x79, 0x0d},
331 {0xc8, 0x20},
332 {0x79, 0x09},
333 {0xc8, 0x80},
334 {0x79, 0x02},
335 {0xc8, 0xc0},
336 {0x79, 0x03},
337 {0xc8, 0x20},
338 {0x79, 0x26},
339};
340static const u8 bridge_start_vga_767x[][2] = {
341/* str59 JPG */
342 {0x94, 0xaa},
343 {0xf1, 0x42},
344 {0xe5, 0x04},
345 {0xc0, 0x50},
346 {0xc1, 0x3c},
347 {0xc2, 0x0c},
348 {0x35, 0x02}, /* turn on JPEG */
349 {0xd9, 0x10},
350 {0xda, 0x00}, /* for higher clock rate(30fps) */
351 {0x34, 0x05}, /* enable Audio Suspend mode */
352 {0xc3, 0xf9}, /* enable PRE */
353 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
354 {0x8d, 0x1c}, /* output YUV */
355/* {0x34, 0x05}, * enable Audio Suspend mode (?) */
356 {0x50, 0x00}, /* H/V divider=0 */
357 {0x51, 0xa0}, /* input H=640/4 */
358 {0x52, 0x3c}, /* input V=480/4 */
359 {0x53, 0x00}, /* offset X=0 */
360 {0x54, 0x00}, /* offset Y=0 */
361 {0x55, 0x00}, /* H/V size[8]=0 */
362 {0x57, 0x00}, /* H-size[9]=0 */
363 {0x5c, 0x00}, /* output size[9:8]=0 */
364 {0x5a, 0xa0}, /* output H=640/4 */
365 {0x5b, 0x78}, /* output V=480/4 */
366 {0x1c, 0x0a},
367 {0x1d, 0x0a},
368 {0x94, 0x11},
369};
370static const u8 sensor_start_vga_767x[][2] = {
371 {0x11, 0x01},
372 {0x1e, 0x04},
373 {0x19, 0x02},
374 {0x1a, 0x7a},
375};
376static const u8 bridge_start_qvga_767x[][2] = {
377/* str86 JPG */
378 {0x94, 0xaa},
379 {0xf1, 0x42},
380 {0xe5, 0x04},
381 {0xc0, 0x80},
382 {0xc1, 0x60},
383 {0xc2, 0x0c},
384 {0x35, 0x02}, /* turn on JPEG */
385 {0xd9, 0x10},
386 {0xc0, 0x50}, /* CIF HSize 640 */
387 {0xc1, 0x3c}, /* CIF VSize 480 */
388 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
389 {0x8d, 0x1c}, /* output YUV */
390 {0x34, 0x05}, /* enable Audio Suspend mode */
391 {0xc2, 0x4c}, /* output YUV and Enable DCW */
392 {0xc3, 0xf9}, /* enable PRE */
393 {0x1c, 0x00}, /* indirect addressing */
394 {0x1d, 0x48}, /* output YUV422 */
395 {0x50, 0x89}, /* H/V divider=/2; plus DCW AVG */
396 {0x51, 0xa0}, /* DCW input H=640/4 */
397 {0x52, 0x78}, /* DCW input V=480/4 */
398 {0x53, 0x00}, /* offset X=0 */
399 {0x54, 0x00}, /* offset Y=0 */
400 {0x55, 0x00}, /* H/V size[8]=0 */
401 {0x57, 0x00}, /* H-size[9]=0 */
402 {0x5c, 0x00}, /* DCW output size[9:8]=0 */
403 {0x5a, 0x50}, /* DCW output H=320/4 */
404 {0x5b, 0x3c}, /* DCW output V=240/4 */
405 {0x1c, 0x0a},
406 {0x1d, 0x0a},
407 {0x94, 0x11},
408};
409static const u8 sensor_start_qvga_767x[][2] = {
410 {0x11, 0x01},
411 {0x1e, 0x04},
412 {0x19, 0x02},
413 {0x1a, 0x7a},
414};
415
416static const u8 bridge_init_772x[][2] = {
Jim Paris47dfd212008-12-04 04:28:27 -0300417 { 0xc2, 0x0c },
418 { 0x88, 0xf8 },
419 { 0xc3, 0x69 },
420 { 0x89, 0xff },
421 { 0x76, 0x03 },
422 { 0x92, 0x01 },
423 { 0x93, 0x18 },
424 { 0x94, 0x10 },
425 { 0x95, 0x10 },
426 { 0xe2, 0x00 },
427 { 0xe7, 0x3e },
428
Jim Paris47dfd212008-12-04 04:28:27 -0300429 { 0x96, 0x00 },
430
431 { 0x97, 0x20 },
432 { 0x97, 0x20 },
433 { 0x97, 0x20 },
434 { 0x97, 0x0a },
435 { 0x97, 0x3f },
436 { 0x97, 0x4a },
437 { 0x97, 0x20 },
438 { 0x97, 0x15 },
439 { 0x97, 0x0b },
440
441 { 0x8e, 0x40 },
442 { 0x1f, 0x81 },
443 { 0x34, 0x05 },
444 { 0xe3, 0x04 },
445 { 0x88, 0x00 },
446 { 0x89, 0x00 },
447 { 0x76, 0x00 },
448 { 0xe7, 0x2e },
449 { 0x31, 0xf9 },
450 { 0x25, 0x42 },
451 { 0x21, 0xf0 },
452
453 { 0x1c, 0x00 },
454 { 0x1d, 0x40 },
Jim Paris0f7a50b2008-12-10 05:45:14 -0300455 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
456 { 0x1d, 0x00 }, /* payload size */
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300457
Jim Paris5ea9c4d2008-12-04 04:36:14 -0300458 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
459 { 0x1d, 0x58 }, /* frame size */
460 { 0x1d, 0x00 }, /* frame size */
Jim Paris47dfd212008-12-04 04:28:27 -0300461
Jim Parisc06eb612008-12-10 05:47:44 -0300462 { 0x1c, 0x0a },
463 { 0x1d, 0x08 }, /* turn on UVC header */
464 { 0x1d, 0x0e }, /* .. */
465
Jim Paris47dfd212008-12-04 04:28:27 -0300466 { 0x8d, 0x1c },
467 { 0x8e, 0x80 },
468 { 0xe5, 0x04 },
469
470 { 0xc0, 0x50 },
471 { 0xc1, 0x3c },
472 { 0xc2, 0x0c },
473};
Jean-François Moine458efe22011-02-10 10:11:04 -0300474static const u8 sensor_init_772x[][2] = {
Jim Paris47dfd212008-12-04 04:28:27 -0300475 { 0x12, 0x80 },
476 { 0x11, 0x01 },
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300477/*fixme: better have a delay?*/
478 { 0x11, 0x01 },
479 { 0x11, 0x01 },
480 { 0x11, 0x01 },
481 { 0x11, 0x01 },
482 { 0x11, 0x01 },
483 { 0x11, 0x01 },
484 { 0x11, 0x01 },
485 { 0x11, 0x01 },
486 { 0x11, 0x01 },
487 { 0x11, 0x01 },
Jim Paris47dfd212008-12-04 04:28:27 -0300488
489 { 0x3d, 0x03 },
490 { 0x17, 0x26 },
491 { 0x18, 0xa0 },
492 { 0x19, 0x07 },
493 { 0x1a, 0xf0 },
494 { 0x32, 0x00 },
495 { 0x29, 0xa0 },
496 { 0x2c, 0xf0 },
497 { 0x65, 0x20 },
498 { 0x11, 0x01 },
499 { 0x42, 0x7f },
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300500 { 0x63, 0xaa }, /* AWB - was e0 */
Jim Paris47dfd212008-12-04 04:28:27 -0300501 { 0x64, 0xff },
502 { 0x66, 0x00 },
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300503 { 0x13, 0xf0 }, /* com8 */
Jim Paris47dfd212008-12-04 04:28:27 -0300504 { 0x0d, 0x41 },
505 { 0x0f, 0xc5 },
506 { 0x14, 0x11 },
507
508 { 0x22, 0x7f },
509 { 0x23, 0x03 },
510 { 0x24, 0x40 },
511 { 0x25, 0x30 },
512 { 0x26, 0xa1 },
513 { 0x2a, 0x00 },
514 { 0x2b, 0x00 },
515 { 0x6b, 0xaa },
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300516 { 0x13, 0xff }, /* AWB */
Jim Paris47dfd212008-12-04 04:28:27 -0300517
518 { 0x90, 0x05 },
519 { 0x91, 0x01 },
520 { 0x92, 0x03 },
521 { 0x93, 0x00 },
522 { 0x94, 0x60 },
523 { 0x95, 0x3c },
524 { 0x96, 0x24 },
525 { 0x97, 0x1e },
526 { 0x98, 0x62 },
527 { 0x99, 0x80 },
528 { 0x9a, 0x1e },
529 { 0x9b, 0x08 },
530 { 0x9c, 0x20 },
531 { 0x9e, 0x81 },
532
Antonio Ospitee89fca92012-05-14 08:07:45 -0300533 { 0xa6, 0x07 },
Jim Paris47dfd212008-12-04 04:28:27 -0300534 { 0x7e, 0x0c },
535 { 0x7f, 0x16 },
536 { 0x80, 0x2a },
537 { 0x81, 0x4e },
538 { 0x82, 0x61 },
539 { 0x83, 0x6f },
540 { 0x84, 0x7b },
541 { 0x85, 0x86 },
542 { 0x86, 0x8e },
543 { 0x87, 0x97 },
544 { 0x88, 0xa4 },
545 { 0x89, 0xaf },
546 { 0x8a, 0xc5 },
547 { 0x8b, 0xd7 },
548 { 0x8c, 0xe8 },
549 { 0x8d, 0x20 },
550
551 { 0x0c, 0x90 },
552
553 { 0x2b, 0x00 },
554 { 0x22, 0x7f },
555 { 0x23, 0x03 },
556 { 0x11, 0x01 },
557 { 0x0c, 0xd0 },
558 { 0x64, 0xff },
559 { 0x0d, 0x41 },
560
561 { 0x14, 0x41 },
562 { 0x0e, 0xcd },
563 { 0xac, 0xbf },
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300564 { 0x8e, 0x00 }, /* De-noise threshold */
Jim Paris47dfd212008-12-04 04:28:27 -0300565 { 0x0c, 0xd0 }
566};
Jean-François Moine458efe22011-02-10 10:11:04 -0300567static const u8 bridge_start_vga_772x[][2] = {
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300568 {0x1c, 0x00},
569 {0x1d, 0x40},
570 {0x1d, 0x02},
571 {0x1d, 0x00},
572 {0x1d, 0x02},
573 {0x1d, 0x58},
574 {0x1d, 0x00},
575 {0xc0, 0x50},
576 {0xc1, 0x3c},
577};
Jean-François Moine458efe22011-02-10 10:11:04 -0300578static const u8 sensor_start_vga_772x[][2] = {
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300579 {0x12, 0x00},
580 {0x17, 0x26},
581 {0x18, 0xa0},
582 {0x19, 0x07},
583 {0x1a, 0xf0},
584 {0x29, 0xa0},
585 {0x2c, 0xf0},
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300586 {0x65, 0x20},
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300587};
Jean-François Moine458efe22011-02-10 10:11:04 -0300588static const u8 bridge_start_qvga_772x[][2] = {
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300589 {0x1c, 0x00},
590 {0x1d, 0x40},
591 {0x1d, 0x02},
592 {0x1d, 0x00},
593 {0x1d, 0x01},
594 {0x1d, 0x4b},
595 {0x1d, 0x00},
596 {0xc0, 0x28},
597 {0xc1, 0x1e},
598};
Jean-François Moine458efe22011-02-10 10:11:04 -0300599static const u8 sensor_start_qvga_772x[][2] = {
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300600 {0x12, 0x40},
601 {0x17, 0x3f},
602 {0x18, 0x50},
603 {0x19, 0x03},
604 {0x1a, 0x78},
605 {0x29, 0x50},
606 {0x2c, 0x78},
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300607 {0x65, 0x2f},
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300608};
Jim Paris47dfd212008-12-04 04:28:27 -0300609
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300610static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
611{
612 struct usb_device *udev = gspca_dev->dev;
613 int ret;
614
Jean-François Moine14b67c22011-01-13 05:58:04 -0300615 if (gspca_dev->usb_err < 0)
616 return;
617
Jean-François Moineddffa492011-01-13 05:49:47 -0300618 PDEBUG(D_USBO, "SET 01 0000 %04x %02x", reg, val);
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300619 gspca_dev->usb_buf[0] = val;
620 ret = usb_control_msg(udev,
621 usb_sndctrlpipe(udev, 0),
622 0x01,
623 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
624 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
Jean-François Moine14b67c22011-01-13 05:58:04 -0300625 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300626 pr_err("write failed %d\n", ret);
Jean-François Moine14b67c22011-01-13 05:58:04 -0300627 gspca_dev->usb_err = ret;
628 }
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300629}
630
631static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
632{
633 struct usb_device *udev = gspca_dev->dev;
634 int ret;
635
Jean-François Moine14b67c22011-01-13 05:58:04 -0300636 if (gspca_dev->usb_err < 0)
637 return 0;
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300638 ret = usb_control_msg(udev,
639 usb_rcvctrlpipe(udev, 0),
640 0x01,
641 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
642 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
Jean-François Moineddffa492011-01-13 05:49:47 -0300643 PDEBUG(D_USBI, "GET 01 0000 %04x %02x", reg, gspca_dev->usb_buf[0]);
Jean-François Moine14b67c22011-01-13 05:58:04 -0300644 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300645 pr_err("read failed %d\n", ret);
Jean-François Moine14b67c22011-01-13 05:58:04 -0300646 gspca_dev->usb_err = ret;
647 }
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300648 return gspca_dev->usb_buf[0];
649}
650
651/* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
652 * (direction and output)? */
653static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
654{
655 u8 data;
656
657 PDEBUG(D_CONF, "led status: %d", status);
658
659 data = ov534_reg_read(gspca_dev, 0x21);
660 data |= 0x80;
661 ov534_reg_write(gspca_dev, 0x21, data);
662
663 data = ov534_reg_read(gspca_dev, 0x23);
664 if (status)
665 data |= 0x80;
666 else
667 data &= ~0x80;
668
669 ov534_reg_write(gspca_dev, 0x23, data);
670
671 if (!status) {
672 data = ov534_reg_read(gspca_dev, 0x21);
673 data &= ~0x80;
674 ov534_reg_write(gspca_dev, 0x21, data);
675 }
676}
677
678static int sccb_check_status(struct gspca_dev *gspca_dev)
679{
680 u8 data;
681 int i;
682
683 for (i = 0; i < 5; i++) {
Jean-Francois Moinef19ed982012-05-28 14:04:07 -0300684 msleep(10);
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300685 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
686
687 switch (data) {
688 case 0x00:
689 return 1;
690 case 0x04:
691 return 0;
692 case 0x03:
693 break;
694 default:
Theodore Kilgorec93396e2013-02-04 13:17:55 -0300695 PERR("sccb status 0x%02x, attempt %d/5",
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300696 data, i + 1);
697 }
698 }
699 return 0;
700}
701
702static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
703{
Jean-François Moineddffa492011-01-13 05:49:47 -0300704 PDEBUG(D_USBO, "sccb write: %02x %02x", reg, val);
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300705 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
706 ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
707 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
708
Jean-François Moine14b67c22011-01-13 05:58:04 -0300709 if (!sccb_check_status(gspca_dev)) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300710 pr_err("sccb_reg_write failed\n");
Jean-François Moine14b67c22011-01-13 05:58:04 -0300711 gspca_dev->usb_err = -EIO;
712 }
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300713}
714
715static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
716{
717 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
718 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
719 if (!sccb_check_status(gspca_dev))
Joe Perches133a9fe2011-08-21 19:56:57 -0300720 pr_err("sccb_reg_read failed 1\n");
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300721
722 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
723 if (!sccb_check_status(gspca_dev))
Joe Perches133a9fe2011-08-21 19:56:57 -0300724 pr_err("sccb_reg_read failed 2\n");
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300725
726 return ov534_reg_read(gspca_dev, OV534_REG_READ);
727}
728
729/* output a bridge sequence (reg - val) */
730static void reg_w_array(struct gspca_dev *gspca_dev,
731 const u8 (*data)[2], int len)
732{
733 while (--len >= 0) {
734 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
735 data++;
736 }
737}
738
739/* output a sensor sequence (reg - val) */
740static void sccb_w_array(struct gspca_dev *gspca_dev,
741 const u8 (*data)[2], int len)
742{
743 while (--len >= 0) {
744 if ((*data)[0] != 0xff) {
745 sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
746 } else {
747 sccb_reg_read(gspca_dev, (*data)[1]);
748 sccb_reg_write(gspca_dev, 0xff, 0x00);
749 }
750 data++;
751 }
752}
753
Jean-Francois Moine69f1fe22009-11-12 06:10:36 -0300754/* ov772x specific controls */
755static void set_frame_rate(struct gspca_dev *gspca_dev)
Jim Paris11d9f252008-12-10 06:06:20 -0300756{
757 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300758 int i;
759 struct rate_s {
760 u8 fps;
761 u8 r11;
762 u8 r0d;
763 u8 re5;
764 };
765 const struct rate_s *r;
766 static const struct rate_s rate_0[] = { /* 640x480 */
767 {60, 0x01, 0xc1, 0x04},
768 {50, 0x01, 0x41, 0x02},
769 {40, 0x02, 0xc1, 0x04},
770 {30, 0x04, 0x81, 0x02},
771 {15, 0x03, 0x41, 0x04},
772 };
773 static const struct rate_s rate_1[] = { /* 320x240 */
Joe Howse0f5b2652014-12-29 12:00:03 -0300774/* {205, 0x01, 0xc1, 0x02}, * 205 FPS: video is partly corrupt */
775 {187, 0x01, 0x81, 0x02}, /* 187 FPS or below: video is valid */
776 {150, 0x01, 0xc1, 0x04},
777 {137, 0x02, 0xc1, 0x02},
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300778 {125, 0x02, 0x81, 0x02},
779 {100, 0x02, 0xc1, 0x04},
780 {75, 0x03, 0xc1, 0x04},
781 {60, 0x04, 0xc1, 0x04},
782 {50, 0x02, 0x41, 0x04},
Joe Howse0f5b2652014-12-29 12:00:03 -0300783 {37, 0x03, 0x41, 0x04},
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300784 {30, 0x04, 0x41, 0x04},
785 };
Jim Paris11d9f252008-12-10 06:06:20 -0300786
Jean-François Moine458efe22011-02-10 10:11:04 -0300787 if (sd->sensor != SENSOR_OV772x)
788 return;
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300789 if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
790 r = rate_0;
791 i = ARRAY_SIZE(rate_0);
792 } else {
793 r = rate_1;
794 i = ARRAY_SIZE(rate_1);
795 }
796 while (--i > 0) {
797 if (sd->frame_rate >= r->fps)
798 break;
799 r++;
Jim Paris11d9f252008-12-10 06:06:20 -0300800 }
801
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300802 sccb_reg_write(gspca_dev, 0x11, r->r11);
803 sccb_reg_write(gspca_dev, 0x0d, r->r0d);
804 ov534_reg_write(gspca_dev, 0xe5, r->re5);
805
806 PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
807}
808
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -0300809static void sethue(struct gspca_dev *gspca_dev, s32 val)
Antonio Ospitee89fca92012-05-14 08:07:45 -0300810{
811 struct sd *sd = (struct sd *) gspca_dev;
Antonio Ospitee89fca92012-05-14 08:07:45 -0300812
Antonio Ospitee89fca92012-05-14 08:07:45 -0300813 if (sd->sensor == SENSOR_OV767x) {
814 /* TBD */
815 } else {
816 s16 huesin;
817 s16 huecos;
818
Antonio Ospitee89fca92012-05-14 08:07:45 -0300819 /* According to the datasheet the registers expect HUESIN and
820 * HUECOS to be the result of the trigonometric functions,
821 * scaled by 0x80.
822 *
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -0300823 * The 0x7fff here represents the maximum absolute value
Antonio Ospitee89fca92012-05-14 08:07:45 -0300824 * returned byt fixp_sin and fixp_cos, so the scaling will
825 * consider the result like in the interval [-1.0, 1.0].
826 */
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -0300827 huesin = fixp_sin16(val) * 0x80 / 0x7fff;
828 huecos = fixp_cos16(val) * 0x80 / 0x7fff;
Antonio Ospitee89fca92012-05-14 08:07:45 -0300829
830 if (huesin < 0) {
831 sccb_reg_write(gspca_dev, 0xab,
832 sccb_reg_read(gspca_dev, 0xab) | 0x2);
833 huesin = -huesin;
834 } else {
835 sccb_reg_write(gspca_dev, 0xab,
836 sccb_reg_read(gspca_dev, 0xab) & ~0x2);
837
838 }
839 sccb_reg_write(gspca_dev, 0xa9, (u8)huecos);
840 sccb_reg_write(gspca_dev, 0xaa, (u8)huesin);
841 }
842}
843
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -0300844static void setsaturation(struct gspca_dev *gspca_dev, s32 val)
Antonio Ospitee0fde592012-05-14 08:07:43 -0300845{
846 struct sd *sd = (struct sd *) gspca_dev;
Antonio Ospitee0fde592012-05-14 08:07:43 -0300847
Antonio Ospitee0fde592012-05-14 08:07:43 -0300848 if (sd->sensor == SENSOR_OV767x) {
849 int i;
850 static u8 color_tb[][6] = {
851 {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
852 {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
853 {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
854 {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
855 {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
856 {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
857 {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
858 };
859
860 for (i = 0; i < ARRAY_SIZE(color_tb[0]); i++)
861 sccb_reg_write(gspca_dev, 0x4f + i, color_tb[val][i]);
862 } else {
863 sccb_reg_write(gspca_dev, 0xa7, val); /* U saturation */
864 sccb_reg_write(gspca_dev, 0xa8, val); /* V saturation */
865 }
866}
867
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -0300868static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300869{
870 struct sd *sd = (struct sd *) gspca_dev;
871
Jean-François Moine458efe22011-02-10 10:11:04 -0300872 if (sd->sensor == SENSOR_OV767x) {
873 if (val < 0)
874 val = 0x80 - val;
875 sccb_reg_write(gspca_dev, 0x55, val); /* bright */
876 } else {
877 sccb_reg_write(gspca_dev, 0x9b, val);
878 }
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300879}
880
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -0300881static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300882{
883 struct sd *sd = (struct sd *) gspca_dev;
884
Jean-François Moine458efe22011-02-10 10:11:04 -0300885 if (sd->sensor == SENSOR_OV767x)
886 sccb_reg_write(gspca_dev, 0x56, val); /* contras */
887 else
888 sccb_reg_write(gspca_dev, 0x9c, val);
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300889}
890
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -0300891static void setgain(struct gspca_dev *gspca_dev, s32 val)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300892{
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300893 switch (val & 0x30) {
894 case 0x00:
895 val &= 0x0f;
896 break;
897 case 0x10:
898 val &= 0x0f;
899 val |= 0x30;
900 break;
901 case 0x20:
902 val &= 0x0f;
903 val |= 0x70;
904 break;
905 default:
906/* case 0x30: */
907 val &= 0x0f;
908 val |= 0xf0;
909 break;
910 }
911 sccb_reg_write(gspca_dev, 0x00, val);
912}
913
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -0300914static s32 getgain(struct gspca_dev *gspca_dev)
915{
916 return sccb_reg_read(gspca_dev, 0x00);
917}
918
919static void setexposure(struct gspca_dev *gspca_dev, s32 val)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300920{
921 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300922
Jean-François Moine458efe22011-02-10 10:11:04 -0300923 if (sd->sensor == SENSOR_OV767x) {
924
925 /* set only aec[9:2] */
926 sccb_reg_write(gspca_dev, 0x10, val); /* aech */
927 } else {
928
929 /* 'val' is one byte and represents half of the exposure value
930 * we are going to set into registers, a two bytes value:
931 *
932 * MSB: ((u16) val << 1) >> 8 == val >> 7
933 * LSB: ((u16) val << 1) & 0xff == val << 1
934 */
935 sccb_reg_write(gspca_dev, 0x08, val >> 7);
936 sccb_reg_write(gspca_dev, 0x10, val << 1);
937 }
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300938}
939
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -0300940static s32 getexposure(struct gspca_dev *gspca_dev)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300941{
942 struct sd *sd = (struct sd *) gspca_dev;
943
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -0300944 if (sd->sensor == SENSOR_OV767x) {
945 /* get only aec[9:2] */
946 return sccb_reg_read(gspca_dev, 0x10); /* aech */
947 } else {
948 u8 hi = sccb_reg_read(gspca_dev, 0x08);
949 u8 lo = sccb_reg_read(gspca_dev, 0x10);
950 return (hi << 8 | lo) >> 1;
951 }
952}
953
954static void setagc(struct gspca_dev *gspca_dev, s32 val)
955{
956 if (val) {
Max Thrun8b7fbda2010-02-27 17:20:20 -0300957 sccb_reg_write(gspca_dev, 0x13,
958 sccb_reg_read(gspca_dev, 0x13) | 0x04);
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300959 sccb_reg_write(gspca_dev, 0x64,
960 sccb_reg_read(gspca_dev, 0x64) | 0x03);
961 } else {
Max Thrun8b7fbda2010-02-27 17:20:20 -0300962 sccb_reg_write(gspca_dev, 0x13,
963 sccb_reg_read(gspca_dev, 0x13) & ~0x04);
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300964 sccb_reg_write(gspca_dev, 0x64,
Max Thrun8b7fbda2010-02-27 17:20:20 -0300965 sccb_reg_read(gspca_dev, 0x64) & ~0x03);
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300966 }
967}
968
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -0300969static void setawb(struct gspca_dev *gspca_dev, s32 val)
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300970{
971 struct sd *sd = (struct sd *) gspca_dev;
972
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -0300973 if (val) {
Max Thrun8fef9d92010-02-27 17:20:23 -0300974 sccb_reg_write(gspca_dev, 0x13,
975 sccb_reg_read(gspca_dev, 0x13) | 0x02);
Jean-François Moine458efe22011-02-10 10:11:04 -0300976 if (sd->sensor == SENSOR_OV772x)
977 sccb_reg_write(gspca_dev, 0x63,
Max Thrun8fef9d92010-02-27 17:20:23 -0300978 sccb_reg_read(gspca_dev, 0x63) | 0xc0);
979 } else {
980 sccb_reg_write(gspca_dev, 0x13,
981 sccb_reg_read(gspca_dev, 0x13) & ~0x02);
Jean-François Moine458efe22011-02-10 10:11:04 -0300982 if (sd->sensor == SENSOR_OV772x)
983 sccb_reg_write(gspca_dev, 0x63,
Max Thrun8fef9d92010-02-27 17:20:23 -0300984 sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
985 }
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300986}
987
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -0300988static void setaec(struct gspca_dev *gspca_dev, s32 val)
Max Thrunf2938822010-02-27 17:20:21 -0300989{
990 struct sd *sd = (struct sd *) gspca_dev;
Jean-François Moine458efe22011-02-10 10:11:04 -0300991 u8 data;
Max Thrunf2938822010-02-27 17:20:21 -0300992
Jean-François Moine458efe22011-02-10 10:11:04 -0300993 data = sd->sensor == SENSOR_OV767x ?
994 0x05 : /* agc + aec */
995 0x01; /* agc */
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -0300996 switch (val) {
997 case V4L2_EXPOSURE_AUTO:
Max Thrunf2938822010-02-27 17:20:21 -0300998 sccb_reg_write(gspca_dev, 0x13,
Jean-François Moine458efe22011-02-10 10:11:04 -0300999 sccb_reg_read(gspca_dev, 0x13) | data);
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -03001000 break;
1001 case V4L2_EXPOSURE_MANUAL:
Max Thrunf2938822010-02-27 17:20:21 -03001002 sccb_reg_write(gspca_dev, 0x13,
Jean-François Moine458efe22011-02-10 10:11:04 -03001003 sccb_reg_read(gspca_dev, 0x13) & ~data);
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -03001004 break;
Max Thrunf2938822010-02-27 17:20:21 -03001005 }
1006}
1007
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -03001008static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001009{
Max Thrun3149cfb2010-02-27 17:20:24 -03001010 sccb_reg_write(gspca_dev, 0x91, val); /* Auto de-noise threshold */
1011 sccb_reg_write(gspca_dev, 0x8e, val); /* De-noise threshold */
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001012}
1013
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -03001014static void sethvflip(struct gspca_dev *gspca_dev, s32 hflip, s32 vflip)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001015{
1016 struct sd *sd = (struct sd *) gspca_dev;
Jean-François Moine228dd262011-02-10 09:34:57 -03001017 u8 val;
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001018
Jean-François Moine458efe22011-02-10 10:11:04 -03001019 if (sd->sensor == SENSOR_OV767x) {
1020 val = sccb_reg_read(gspca_dev, 0x1e); /* mvfp */
1021 val &= ~0x30;
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -03001022 if (hflip)
Jean-François Moine458efe22011-02-10 10:11:04 -03001023 val |= 0x20;
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -03001024 if (vflip)
Jean-François Moine458efe22011-02-10 10:11:04 -03001025 val |= 0x10;
1026 sccb_reg_write(gspca_dev, 0x1e, val);
1027 } else {
1028 val = sccb_reg_read(gspca_dev, 0x0c);
1029 val &= ~0xc0;
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -03001030 if (hflip == 0)
Jean-François Moine458efe22011-02-10 10:11:04 -03001031 val |= 0x40;
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -03001032 if (vflip == 0)
Jean-François Moine458efe22011-02-10 10:11:04 -03001033 val |= 0x80;
1034 sccb_reg_write(gspca_dev, 0x0c, val);
1035 }
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001036}
1037
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -03001038static void setlightfreq(struct gspca_dev *gspca_dev, s32 val)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001039{
1040 struct sd *sd = (struct sd *) gspca_dev;
1041
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -03001042 val = val ? 0x9e : 0x00;
Jean-François Moine458efe22011-02-10 10:11:04 -03001043 if (sd->sensor == SENSOR_OV767x) {
1044 sccb_reg_write(gspca_dev, 0x2a, 0x00);
1045 if (val)
1046 val = 0x9d; /* insert dummy to 25fps for 50Hz */
1047 }
1048 sccb_reg_write(gspca_dev, 0x2b, val);
Mosalam Ebrahimi4b787542010-03-08 13:52:17 -03001049}
1050
1051
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001052/* this function is called at probe time */
1053static int sd_config(struct gspca_dev *gspca_dev,
1054 const struct usb_device_id *id)
1055{
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -03001056 struct sd *sd = (struct sd *) gspca_dev;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001057 struct cam *cam;
1058
1059 cam = &gspca_dev->cam;
1060
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001061 cam->cam_mode = ov772x_mode;
1062 cam->nmodes = ARRAY_SIZE(ov772x_mode);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001063
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001064 sd->frame_rate = 30;
Jean-Francois Moineb014f942009-11-11 14:28:53 -03001065
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001066 return 0;
1067}
1068
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -03001069static int ov534_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1070{
1071 struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
1072 struct gspca_dev *gspca_dev = &sd->gspca_dev;
1073
1074 switch (ctrl->id) {
1075 case V4L2_CID_AUTOGAIN:
1076 gspca_dev->usb_err = 0;
1077 if (ctrl->val && sd->gain && gspca_dev->streaming)
1078 sd->gain->val = getgain(gspca_dev);
1079 return gspca_dev->usb_err;
1080
1081 case V4L2_CID_EXPOSURE_AUTO:
1082 gspca_dev->usb_err = 0;
1083 if (ctrl->val == V4L2_EXPOSURE_AUTO && sd->exposure &&
1084 gspca_dev->streaming)
1085 sd->exposure->val = getexposure(gspca_dev);
1086 return gspca_dev->usb_err;
1087 }
1088 return -EINVAL;
1089}
1090
1091static int ov534_s_ctrl(struct v4l2_ctrl *ctrl)
1092{
1093 struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
1094 struct gspca_dev *gspca_dev = &sd->gspca_dev;
1095
1096 gspca_dev->usb_err = 0;
1097 if (!gspca_dev->streaming)
1098 return 0;
1099
1100 switch (ctrl->id) {
1101 case V4L2_CID_HUE:
1102 sethue(gspca_dev, ctrl->val);
1103 break;
1104 case V4L2_CID_SATURATION:
1105 setsaturation(gspca_dev, ctrl->val);
1106 break;
1107 case V4L2_CID_BRIGHTNESS:
1108 setbrightness(gspca_dev, ctrl->val);
1109 break;
1110 case V4L2_CID_CONTRAST:
1111 setcontrast(gspca_dev, ctrl->val);
1112 break;
1113 case V4L2_CID_AUTOGAIN:
1114 /* case V4L2_CID_GAIN: */
1115 setagc(gspca_dev, ctrl->val);
1116 if (!gspca_dev->usb_err && !ctrl->val && sd->gain)
1117 setgain(gspca_dev, sd->gain->val);
1118 break;
1119 case V4L2_CID_AUTO_WHITE_BALANCE:
1120 setawb(gspca_dev, ctrl->val);
1121 break;
1122 case V4L2_CID_EXPOSURE_AUTO:
1123 /* case V4L2_CID_EXPOSURE: */
1124 setaec(gspca_dev, ctrl->val);
1125 if (!gspca_dev->usb_err && ctrl->val == V4L2_EXPOSURE_MANUAL &&
1126 sd->exposure)
1127 setexposure(gspca_dev, sd->exposure->val);
1128 break;
1129 case V4L2_CID_SHARPNESS:
1130 setsharpness(gspca_dev, ctrl->val);
1131 break;
1132 case V4L2_CID_HFLIP:
1133 sethvflip(gspca_dev, ctrl->val, sd->vflip->val);
1134 break;
1135 case V4L2_CID_VFLIP:
1136 sethvflip(gspca_dev, sd->hflip->val, ctrl->val);
1137 break;
1138 case V4L2_CID_POWER_LINE_FREQUENCY:
1139 setlightfreq(gspca_dev, ctrl->val);
1140 break;
1141 }
1142 return gspca_dev->usb_err;
1143}
1144
1145static const struct v4l2_ctrl_ops ov534_ctrl_ops = {
1146 .g_volatile_ctrl = ov534_g_volatile_ctrl,
1147 .s_ctrl = ov534_s_ctrl,
1148};
1149
1150static int sd_init_controls(struct gspca_dev *gspca_dev)
1151{
1152 struct sd *sd = (struct sd *) gspca_dev;
1153 struct v4l2_ctrl_handler *hdl = &sd->ctrl_handler;
1154 /* parameters with different values between the supported sensors */
1155 int saturation_min;
1156 int saturation_max;
1157 int saturation_def;
1158 int brightness_min;
1159 int brightness_max;
1160 int brightness_def;
1161 int contrast_max;
1162 int contrast_def;
1163 int exposure_min;
1164 int exposure_max;
1165 int exposure_def;
1166 int hflip_def;
1167
1168 if (sd->sensor == SENSOR_OV767x) {
1169 saturation_min = 0,
1170 saturation_max = 6,
1171 saturation_def = 3,
1172 brightness_min = -127;
1173 brightness_max = 127;
1174 brightness_def = 0;
1175 contrast_max = 0x80;
1176 contrast_def = 0x40;
1177 exposure_min = 0x08;
1178 exposure_max = 0x60;
1179 exposure_def = 0x13;
1180 hflip_def = 1;
1181 } else {
1182 saturation_min = 0,
1183 saturation_max = 255,
1184 saturation_def = 64,
1185 brightness_min = 0;
1186 brightness_max = 255;
1187 brightness_def = 0;
1188 contrast_max = 255;
1189 contrast_def = 32;
1190 exposure_min = 0;
1191 exposure_max = 255;
1192 exposure_def = 120;
1193 hflip_def = 0;
1194 }
1195
1196 gspca_dev->vdev.ctrl_handler = hdl;
1197
1198 v4l2_ctrl_handler_init(hdl, 13);
1199
1200 if (sd->sensor == SENSOR_OV772x)
1201 sd->hue = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1202 V4L2_CID_HUE, -90, 90, 1, 0);
1203
1204 sd->saturation = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1205 V4L2_CID_SATURATION, saturation_min, saturation_max, 1,
1206 saturation_def);
1207 sd->brightness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1208 V4L2_CID_BRIGHTNESS, brightness_min, brightness_max, 1,
1209 brightness_def);
1210 sd->contrast = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1211 V4L2_CID_CONTRAST, 0, contrast_max, 1, contrast_def);
1212
1213 if (sd->sensor == SENSOR_OV772x) {
1214 sd->autogain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1215 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1216 sd->gain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1217 V4L2_CID_GAIN, 0, 63, 1, 20);
1218 }
1219
1220 sd->autoexposure = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
1221 V4L2_CID_EXPOSURE_AUTO,
1222 V4L2_EXPOSURE_MANUAL, 0,
1223 V4L2_EXPOSURE_AUTO);
1224 sd->exposure = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1225 V4L2_CID_EXPOSURE, exposure_min, exposure_max, 1,
1226 exposure_def);
1227
1228 sd->autowhitebalance = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1229 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
1230
1231 if (sd->sensor == SENSOR_OV772x)
1232 sd->sharpness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1233 V4L2_CID_SHARPNESS, 0, 63, 1, 0);
1234
1235 sd->hflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1236 V4L2_CID_HFLIP, 0, 1, 1, hflip_def);
1237 sd->vflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1238 V4L2_CID_VFLIP, 0, 1, 1, 0);
1239 sd->plfreq = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
1240 V4L2_CID_POWER_LINE_FREQUENCY,
1241 V4L2_CID_POWER_LINE_FREQUENCY_50HZ, 0,
1242 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED);
1243
1244 if (hdl->error) {
1245 pr_err("Could not initialize controls\n");
1246 return hdl->error;
1247 }
1248
1249 if (sd->sensor == SENSOR_OV772x)
1250 v4l2_ctrl_auto_cluster(2, &sd->autogain, 0, true);
1251
1252 v4l2_ctrl_auto_cluster(2, &sd->autoexposure, V4L2_EXPOSURE_MANUAL,
1253 true);
1254
1255 return 0;
1256}
1257
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001258/* this function is called at probe and resume time */
1259static int sd_init(struct gspca_dev *gspca_dev)
1260{
Jean-François Moine458efe22011-02-10 10:11:04 -03001261 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -03001262 u16 sensor_id;
Jean-François Moine458efe22011-02-10 10:11:04 -03001263 static const struct reg_array bridge_init[NSENSORS] = {
1264 [SENSOR_OV767x] = {bridge_init_767x, ARRAY_SIZE(bridge_init_767x)},
1265 [SENSOR_OV772x] = {bridge_init_772x, ARRAY_SIZE(bridge_init_772x)},
1266 };
1267 static const struct reg_array sensor_init[NSENSORS] = {
1268 [SENSOR_OV767x] = {sensor_init_767x, ARRAY_SIZE(sensor_init_767x)},
1269 [SENSOR_OV772x] = {sensor_init_772x, ARRAY_SIZE(sensor_init_772x)},
1270 };
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -03001271
1272 /* reset bridge */
1273 ov534_reg_write(gspca_dev, 0xe7, 0x3a);
1274 ov534_reg_write(gspca_dev, 0xe0, 0x08);
1275 msleep(100);
1276
1277 /* initialize the sensor address */
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001278 ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -03001279
1280 /* reset sensor */
1281 sccb_reg_write(gspca_dev, 0x12, 0x80);
1282 msleep(10);
1283
1284 /* probe the sensor */
1285 sccb_reg_read(gspca_dev, 0x0a);
1286 sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
1287 sccb_reg_read(gspca_dev, 0x0b);
1288 sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
1289 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
1290
Jean-François Moine458efe22011-02-10 10:11:04 -03001291 if ((sensor_id & 0xfff0) == 0x7670) {
1292 sd->sensor = SENSOR_OV767x;
Jean-François Moine458efe22011-02-10 10:11:04 -03001293 gspca_dev->cam.cam_mode = ov767x_mode;
1294 gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode);
1295 } else {
1296 sd->sensor = SENSOR_OV772x;
1297 gspca_dev->cam.bulk = 1;
1298 gspca_dev->cam.bulk_size = 16384;
1299 gspca_dev->cam.bulk_nurbs = 2;
1300 gspca_dev->cam.mode_framerates = ov772x_framerates;
1301 }
1302
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -03001303 /* initialize */
Jean-François Moine458efe22011-02-10 10:11:04 -03001304 reg_w_array(gspca_dev, bridge_init[sd->sensor].val,
1305 bridge_init[sd->sensor].len);
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001306 ov534_set_led(gspca_dev, 1);
Jean-François Moine458efe22011-02-10 10:11:04 -03001307 sccb_w_array(gspca_dev, sensor_init[sd->sensor].val,
1308 sensor_init[sd->sensor].len);
Antonio Ospited48de1c2013-08-15 07:29:32 -03001309
Jean-François Moine458efe22011-02-10 10:11:04 -03001310 sd_stopN(gspca_dev);
1311/* set_frame_rate(gspca_dev); */
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001312
Jean-François Moine14b67c22011-01-13 05:58:04 -03001313 return gspca_dev->usb_err;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001314}
1315
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001316static int sd_start(struct gspca_dev *gspca_dev)
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001317{
Jean-François Moine458efe22011-02-10 10:11:04 -03001318 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine191d0e72009-05-22 04:16:42 -03001319 int mode;
Jean-François Moine458efe22011-02-10 10:11:04 -03001320 static const struct reg_array bridge_start[NSENSORS][2] = {
1321 [SENSOR_OV767x] = {{bridge_start_qvga_767x,
1322 ARRAY_SIZE(bridge_start_qvga_767x)},
1323 {bridge_start_vga_767x,
1324 ARRAY_SIZE(bridge_start_vga_767x)}},
1325 [SENSOR_OV772x] = {{bridge_start_qvga_772x,
1326 ARRAY_SIZE(bridge_start_qvga_772x)},
1327 {bridge_start_vga_772x,
1328 ARRAY_SIZE(bridge_start_vga_772x)}},
1329 };
1330 static const struct reg_array sensor_start[NSENSORS][2] = {
1331 [SENSOR_OV767x] = {{sensor_start_qvga_767x,
1332 ARRAY_SIZE(sensor_start_qvga_767x)},
1333 {sensor_start_vga_767x,
1334 ARRAY_SIZE(sensor_start_vga_767x)}},
1335 [SENSOR_OV772x] = {{sensor_start_qvga_772x,
1336 ARRAY_SIZE(sensor_start_qvga_772x)},
1337 {sensor_start_vga_772x,
1338 ARRAY_SIZE(sensor_start_vga_772x)}},
1339 };
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001340
Jean-François Moine458efe22011-02-10 10:11:04 -03001341 /* (from ms-win trace) */
1342 if (sd->sensor == SENSOR_OV767x)
1343 sccb_reg_write(gspca_dev, 0x1e, 0x04);
1344 /* black sun enable ? */
1345
1346 mode = gspca_dev->curr_mode; /* 0: 320x240, 1: 640x480 */
1347 reg_w_array(gspca_dev, bridge_start[sd->sensor][mode].val,
1348 bridge_start[sd->sensor][mode].len);
1349 sccb_w_array(gspca_dev, sensor_start[sd->sensor][mode].val,
1350 sensor_start[sd->sensor][mode].len);
1351
Jean-Francois Moine69f1fe22009-11-12 06:10:36 -03001352 set_frame_rate(gspca_dev);
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001353
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -03001354 if (sd->hue)
1355 sethue(gspca_dev, v4l2_ctrl_g_ctrl(sd->hue));
1356 setsaturation(gspca_dev, v4l2_ctrl_g_ctrl(sd->saturation));
1357 if (sd->autogain)
1358 setagc(gspca_dev, v4l2_ctrl_g_ctrl(sd->autogain));
1359 setawb(gspca_dev, v4l2_ctrl_g_ctrl(sd->autowhitebalance));
1360 setaec(gspca_dev, v4l2_ctrl_g_ctrl(sd->autoexposure));
1361 if (sd->gain)
1362 setgain(gspca_dev, v4l2_ctrl_g_ctrl(sd->gain));
1363 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure));
1364 setbrightness(gspca_dev, v4l2_ctrl_g_ctrl(sd->brightness));
1365 setcontrast(gspca_dev, v4l2_ctrl_g_ctrl(sd->contrast));
1366 if (sd->sharpness)
1367 setsharpness(gspca_dev, v4l2_ctrl_g_ctrl(sd->sharpness));
1368 sethvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip),
1369 v4l2_ctrl_g_ctrl(sd->vflip));
1370 setlightfreq(gspca_dev, v4l2_ctrl_g_ctrl(sd->plfreq));
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001371
1372 ov534_set_led(gspca_dev, 1);
1373 ov534_reg_write(gspca_dev, 0xe0, 0x00);
Jean-François Moine14b67c22011-01-13 05:58:04 -03001374 return gspca_dev->usb_err;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001375}
1376
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001377static void sd_stopN(struct gspca_dev *gspca_dev)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001378{
1379 ov534_reg_write(gspca_dev, 0xe0, 0x09);
1380 ov534_set_led(gspca_dev, 0);
1381}
1382
Jim Parisfb139222008-12-04 04:52:40 -03001383/* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1384#define UVC_STREAM_EOH (1 << 7)
1385#define UVC_STREAM_ERR (1 << 6)
1386#define UVC_STREAM_STI (1 << 5)
1387#define UVC_STREAM_RES (1 << 4)
1388#define UVC_STREAM_SCR (1 << 3)
1389#define UVC_STREAM_PTS (1 << 2)
1390#define UVC_STREAM_EOF (1 << 1)
1391#define UVC_STREAM_FID (1 << 0)
1392
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001393static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1394 u8 *data, int len)
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001395{
Jean-Francois Moine8c252052008-12-04 05:06:08 -03001396 struct sd *sd = (struct sd *) gspca_dev;
Jim Parisfb139222008-12-04 04:52:40 -03001397 __u32 this_pts;
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001398 u16 this_fid;
Jim Paris0f7a50b2008-12-10 05:45:14 -03001399 int remaining_len = len;
Jean-François Moine458efe22011-02-10 10:11:04 -03001400 int payload_len;
Jim Paris0f7a50b2008-12-10 05:45:14 -03001401
Jean-François Moine458efe22011-02-10 10:11:04 -03001402 payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001403 do {
Jean-François Moine458efe22011-02-10 10:11:04 -03001404 len = min(remaining_len, payload_len);
Jim Paris0f7a50b2008-12-10 05:45:14 -03001405
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001406 /* Payloads are prefixed with a UVC-style header. We
1407 consider a frame to start when the FID toggles, or the PTS
1408 changes. A frame ends when EOF is set, and we've received
1409 the correct number of bytes. */
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001410
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001411 /* Verify UVC header. Header length is always 12 */
1412 if (data[0] != 12 || len < 12) {
1413 PDEBUG(D_PACK, "bad header");
Jim Parisfb139222008-12-04 04:52:40 -03001414 goto discard;
1415 }
1416
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001417 /* Check errors */
1418 if (data[1] & UVC_STREAM_ERR) {
1419 PDEBUG(D_PACK, "payload error");
1420 goto discard;
Jean-Francois Moine3481c192009-03-19 06:05:06 -03001421 }
Jim Parisfb139222008-12-04 04:52:40 -03001422
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001423 /* Extract PTS and FID */
1424 if (!(data[1] & UVC_STREAM_PTS)) {
1425 PDEBUG(D_PACK, "PTS not present");
1426 goto discard;
1427 }
1428 this_pts = (data[5] << 24) | (data[4] << 16)
1429 | (data[3] << 8) | data[2];
1430 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
1431
1432 /* If PTS or FID has changed, start a new frame. */
1433 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
Jean-Francois Moineed471192009-04-23 13:52:27 -03001434 if (gspca_dev->last_packet_type == INTER_PACKET)
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001435 gspca_frame_add(gspca_dev, LAST_PACKET,
1436 NULL, 0);
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001437 sd->last_pts = this_pts;
1438 sd->last_fid = this_fid;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001439 gspca_frame_add(gspca_dev, FIRST_PACKET,
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001440 data + 12, len - 12);
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001441 /* If this packet is marked as EOF, end the frame */
Jean-Francois Moineed471192009-04-23 13:52:27 -03001442 } else if (data[1] & UVC_STREAM_EOF) {
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001443 sd->last_pts = 0;
Ondrej Zary1966bc22013-08-30 17:54:23 -03001444 if (gspca_dev->pixfmt.pixelformat == V4L2_PIX_FMT_YUYV
Jean-François Moine458efe22011-02-10 10:11:04 -03001445 && gspca_dev->image_len + len - 12 !=
Ondrej Zary1966bc22013-08-30 17:54:23 -03001446 gspca_dev->pixfmt.width *
1447 gspca_dev->pixfmt.height * 2) {
Antonio Ospite11edebc2010-01-17 03:42:14 -03001448 PDEBUG(D_PACK, "wrong sized frame");
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001449 goto discard;
1450 }
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001451 gspca_frame_add(gspca_dev, LAST_PACKET,
1452 data + 12, len - 12);
Jean-Francois Moineed471192009-04-23 13:52:27 -03001453 } else {
1454
1455 /* Add the data from this payload */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001456 gspca_frame_add(gspca_dev, INTER_PACKET,
1457 data + 12, len - 12);
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001458 }
1459
1460 /* Done this payload */
1461 goto scan_next;
Jim Parisfb139222008-12-04 04:52:40 -03001462
1463discard:
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001464 /* Discard data until a new frame starts. */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001465 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001466
1467scan_next:
1468 remaining_len -= len;
1469 data += len;
1470 } while (remaining_len > 0);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001471}
1472
Jim Paris11d9f252008-12-10 06:06:20 -03001473/* get stream parameters (framerate) */
Jean-François Moine668f44a2010-12-25 13:46:14 -03001474static void sd_get_streamparm(struct gspca_dev *gspca_dev,
Jean-Francois Moine5c1a15a2008-12-21 15:02:54 -03001475 struct v4l2_streamparm *parm)
Jim Paris11d9f252008-12-10 06:06:20 -03001476{
1477 struct v4l2_captureparm *cp = &parm->parm.capture;
1478 struct v4l2_fract *tpf = &cp->timeperframe;
1479 struct sd *sd = (struct sd *) gspca_dev;
1480
Jim Paris11d9f252008-12-10 06:06:20 -03001481 cp->capability |= V4L2_CAP_TIMEPERFRAME;
1482 tpf->numerator = 1;
1483 tpf->denominator = sd->frame_rate;
Jim Paris11d9f252008-12-10 06:06:20 -03001484}
1485
1486/* set stream parameters (framerate) */
Jean-François Moine668f44a2010-12-25 13:46:14 -03001487static void sd_set_streamparm(struct gspca_dev *gspca_dev,
Jean-Francois Moine5c1a15a2008-12-21 15:02:54 -03001488 struct v4l2_streamparm *parm)
Jim Paris11d9f252008-12-10 06:06:20 -03001489{
1490 struct v4l2_captureparm *cp = &parm->parm.capture;
1491 struct v4l2_fract *tpf = &cp->timeperframe;
1492 struct sd *sd = (struct sd *) gspca_dev;
1493
Antonio Ospitedcc7fdb2015-10-02 17:33:13 -03001494 if (tpf->numerator == 0 || tpf->denominator == 0)
1495 /* Set default framerate */
1496 sd->frame_rate = 30;
1497 else
1498 /* Set requested framerate */
1499 sd->frame_rate = tpf->denominator / tpf->numerator;
1500
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001501 if (gspca_dev->streaming)
Jean-Francois Moine69f1fe22009-11-12 06:10:36 -03001502 set_frame_rate(gspca_dev);
Jim Paris11d9f252008-12-10 06:06:20 -03001503
1504 /* Return the actual framerate */
1505 tpf->numerator = 1;
1506 tpf->denominator = sd->frame_rate;
Jim Paris11d9f252008-12-10 06:06:20 -03001507}
1508
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001509/* sub-driver description */
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001510static const struct sd_desc sd_desc = {
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001511 .name = MODULE_NAME,
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001512 .config = sd_config,
1513 .init = sd_init,
Antonio Ospite1bd7d6a2012-05-16 18:42:46 -03001514 .init_controls = sd_init_controls,
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001515 .start = sd_start,
1516 .stopN = sd_stopN,
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001517 .pkt_scan = sd_pkt_scan,
1518 .get_streamparm = sd_get_streamparm,
1519 .set_streamparm = sd_set_streamparm,
1520};
1521
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001522/* -- module initialisation -- */
Jean-François Moine95c967c2011-01-13 05:20:29 -03001523static const struct usb_device_id device_table[] = {
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001524 {USB_DEVICE(0x1415, 0x2000)},
Jean-François Moine458efe22011-02-10 10:11:04 -03001525 {USB_DEVICE(0x06f8, 0x3002)},
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001526 {}
1527};
1528
1529MODULE_DEVICE_TABLE(usb, device_table);
1530
1531/* -- device connect -- */
1532static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1533{
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001534 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001535 THIS_MODULE);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001536}
1537
1538static struct usb_driver sd_driver = {
1539 .name = MODULE_NAME,
1540 .id_table = device_table,
1541 .probe = sd_probe,
1542 .disconnect = gspca_disconnect,
1543#ifdef CONFIG_PM
1544 .suspend = gspca_suspend,
1545 .resume = gspca_resume,
Hans de Goede8bb58962012-06-30 06:44:47 -03001546 .reset_resume = gspca_resume,
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001547#endif
1548};
1549
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08001550module_usb_driver(sd_driver);