Merge branch 'for-2.6.30' into for-2.6.31
[linux-2.6] / drivers / media / video / gspca / ov534.c
1 /*
2  * ov534 gspca driver
3  * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
4  * Copyright (C) 2008 Jim Paris <jim@jtan.com>
5  * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
6  *
7  * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
8  * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
9  * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  */
25
26 #define MODULE_NAME "ov534"
27
28 #include "gspca.h"
29
30 #define OV534_REG_ADDRESS       0xf1    /* sensor address */
31 #define OV534_REG_SUBADDR       0xf2
32 #define OV534_REG_WRITE         0xf3
33 #define OV534_REG_READ          0xf4
34 #define OV534_REG_OPERATION     0xf5
35 #define OV534_REG_STATUS        0xf6
36
37 #define OV534_OP_WRITE_3        0x37
38 #define OV534_OP_WRITE_2        0x33
39 #define OV534_OP_READ_2         0xf9
40
41 #define CTRL_TIMEOUT 500
42
43 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
44 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
45 MODULE_LICENSE("GPL");
46
47 /* specific webcam descriptor */
48 struct sd {
49         struct gspca_dev gspca_dev;     /* !! must be the first item */
50         __u32 last_pts;
51         u16 last_fid;
52         u8 frame_rate;
53
54         u8 sensor;
55 #define SENSOR_OV772X 0
56 #define SENSOR_OV965X 1
57 };
58
59 /* V4L2 controls supported by the driver */
60 static struct ctrl sd_ctrls[] = {
61 };
62
63 static const struct v4l2_pix_format vga_mode[] = {
64         {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
65          .bytesperline = 640 * 2,
66          .sizeimage = 640 * 480 * 2,
67          .colorspace = V4L2_COLORSPACE_JPEG,
68          .priv = 0},
69 };
70
71 static const u8 bridge_init_ov722x[][2] = {
72         { 0xc2, 0x0c },
73         { 0x88, 0xf8 },
74         { 0xc3, 0x69 },
75         { 0x89, 0xff },
76         { 0x76, 0x03 },
77         { 0x92, 0x01 },
78         { 0x93, 0x18 },
79         { 0x94, 0x10 },
80         { 0x95, 0x10 },
81         { 0xe2, 0x00 },
82         { 0xe7, 0x3e },
83
84         { 0x96, 0x00 },
85
86         { 0x97, 0x20 },
87         { 0x97, 0x20 },
88         { 0x97, 0x20 },
89         { 0x97, 0x0a },
90         { 0x97, 0x3f },
91         { 0x97, 0x4a },
92         { 0x97, 0x20 },
93         { 0x97, 0x15 },
94         { 0x97, 0x0b },
95
96         { 0x8e, 0x40 },
97         { 0x1f, 0x81 },
98         { 0x34, 0x05 },
99         { 0xe3, 0x04 },
100         { 0x88, 0x00 },
101         { 0x89, 0x00 },
102         { 0x76, 0x00 },
103         { 0xe7, 0x2e },
104         { 0x31, 0xf9 },
105         { 0x25, 0x42 },
106         { 0x21, 0xf0 },
107
108         { 0x1c, 0x00 },
109         { 0x1d, 0x40 },
110         { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
111         { 0x1d, 0x00 }, /* payload size */
112         { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
113         { 0x1d, 0x58 }, /* frame size */
114         { 0x1d, 0x00 }, /* frame size */
115
116         { 0x1c, 0x0a },
117         { 0x1d, 0x08 }, /* turn on UVC header */
118         { 0x1d, 0x0e }, /* .. */
119
120         { 0x8d, 0x1c },
121         { 0x8e, 0x80 },
122         { 0xe5, 0x04 },
123
124         { 0xc0, 0x50 },
125         { 0xc1, 0x3c },
126         { 0xc2, 0x0c },
127 };
128
129 static const u8 sensor_init_ov722x[][2] = {
130         { 0x12, 0x80 },
131         { 0x11, 0x01 },
132
133         { 0x3d, 0x03 },
134         { 0x17, 0x26 },
135         { 0x18, 0xa0 },
136         { 0x19, 0x07 },
137         { 0x1a, 0xf0 },
138         { 0x32, 0x00 },
139         { 0x29, 0xa0 },
140         { 0x2c, 0xf0 },
141         { 0x65, 0x20 },
142         { 0x11, 0x01 },
143         { 0x42, 0x7f },
144         { 0x63, 0xe0 },
145         { 0x64, 0xff },
146         { 0x66, 0x00 },
147         { 0x13, 0xf0 },
148         { 0x0d, 0x41 },
149         { 0x0f, 0xc5 },
150         { 0x14, 0x11 },
151
152         { 0x22, 0x7f },
153         { 0x23, 0x03 },
154         { 0x24, 0x40 },
155         { 0x25, 0x30 },
156         { 0x26, 0xa1 },
157         { 0x2a, 0x00 },
158         { 0x2b, 0x00 },
159         { 0x6b, 0xaa },
160         { 0x13, 0xff },
161
162         { 0x90, 0x05 },
163         { 0x91, 0x01 },
164         { 0x92, 0x03 },
165         { 0x93, 0x00 },
166         { 0x94, 0x60 },
167         { 0x95, 0x3c },
168         { 0x96, 0x24 },
169         { 0x97, 0x1e },
170         { 0x98, 0x62 },
171         { 0x99, 0x80 },
172         { 0x9a, 0x1e },
173         { 0x9b, 0x08 },
174         { 0x9c, 0x20 },
175         { 0x9e, 0x81 },
176
177         { 0xa6, 0x04 },
178         { 0x7e, 0x0c },
179         { 0x7f, 0x16 },
180         { 0x80, 0x2a },
181         { 0x81, 0x4e },
182         { 0x82, 0x61 },
183         { 0x83, 0x6f },
184         { 0x84, 0x7b },
185         { 0x85, 0x86 },
186         { 0x86, 0x8e },
187         { 0x87, 0x97 },
188         { 0x88, 0xa4 },
189         { 0x89, 0xaf },
190         { 0x8a, 0xc5 },
191         { 0x8b, 0xd7 },
192         { 0x8c, 0xe8 },
193         { 0x8d, 0x20 },
194
195         { 0x0c, 0x90 },
196
197         { 0x2b, 0x00 },
198         { 0x22, 0x7f },
199         { 0x23, 0x03 },
200         { 0x11, 0x01 },
201         { 0x0c, 0xd0 },
202         { 0x64, 0xff },
203         { 0x0d, 0x41 },
204
205         { 0x14, 0x41 },
206         { 0x0e, 0xcd },
207         { 0xac, 0xbf },
208         { 0x8e, 0x00 },
209         { 0x0c, 0xd0 }
210 };
211
212 static const u8 bridge_init_ov965x[][2] = {
213         {0x88, 0xf8},
214         {0x89, 0xff},
215         {0x76, 0x03},
216         {0x92, 0x03},
217         {0x95, 0x10},
218         {0xe2, 0x00},
219         {0xe7, 0x3e},
220         {0x8d, 0x1c},
221         {0x8e, 0x00},
222         {0x8f, 0x00},
223         {0x1f, 0x00},
224         {0xc3, 0xf9},
225         {0x89, 0xff},
226         {0x88, 0xf8},
227         {0x76, 0x03},
228         {0x92, 0x01},
229         {0x93, 0x18},
230         {0x1c, 0x0a},
231         {0x1d, 0x48},
232         {0xc0, 0x50},
233         {0xc1, 0x3c},
234         {0x34, 0x05},
235         {0xc2, 0x0c},
236         {0xc3, 0xf9},
237         {0x34, 0x05},
238         {0xe7, 0x2e},
239         {0x31, 0xf9},
240         {0x35, 0x02},
241         {0xd9, 0x10},
242         {0x25, 0x42},
243         {0x94, 0x11},
244 };
245
246 static const u8 sensor_init_ov965x[][2] = {
247         {0x12, 0x80},   /* com7 - reset */
248         {0x00, 0x00},   /* gain */
249         {0x01, 0x80},   /* blue */
250         {0x02, 0x80},   /* red */
251         {0x03, 0x1b},   /* vref */
252         {0x04, 0x03},   /* com1 - exposure low bits */
253         {0x0b, 0x57},   /* ver */
254         {0x0e, 0x61},   /* com5 */
255         {0x0f, 0x42},   /* com6 */
256         {0x11, 0x00},   /* clkrc */
257         {0x12, 0x02},   /* com7 */
258         {0x13, 0xe7},   /* com8 - everything (AGC, AWB and AEC) */
259         {0x14, 0x28},   /* com9 */
260         {0x16, 0x24},   /* rsvd16 */
261         {0x17, 0x1d},   /* hstart*/
262         {0x18, 0xbd},   /* hstop */
263         {0x19, 0x01},   /* vstrt */
264         {0x1a, 0x81},   /* vstop*/
265         {0x1e, 0x04},   /* mvfp */
266         {0x24, 0x3c},   /* aew */
267         {0x25, 0x36},   /* aeb */
268         {0x26, 0x71},   /* vpt */
269         {0x27, 0x08},   /* bbias */
270         {0x28, 0x08},   /* gbbias */
271         {0x29, 0x15},   /* gr com */
272         {0x2a, 0x00},
273         {0x2b, 0x00},
274         {0x2c, 0x08},   /* rbias */
275         {0x32, 0xff},   /* href */
276         {0x33, 0x00},   /* chlf */
277         {0x34, 0x3f},   /* arblm */
278         {0x35, 0x00},   /* rsvd35 */
279         {0x36, 0xf8},   /* rsvd36 */
280         {0x38, 0x72},   /* acom38 */
281         {0x39, 0x57},   /* ofon */
282         {0x3a, 0x80},   /* tslb */
283         {0x3b, 0xc4},
284         {0x3d, 0x99},   /* com13 */
285         {0x3f, 0xc1},
286         {0x40, 0xc0},   /* com15 */
287         {0x41, 0x40},   /* com16 */
288         {0x42, 0xc0},
289         {0x43, 0x0a},
290         {0x44, 0xf0},
291         {0x45, 0x46},
292         {0x46, 0x62},
293         {0x47, 0x2a},
294         {0x48, 0x3c},
295         {0x4a, 0xfc},
296         {0x4b, 0xfc},
297         {0x4c, 0x7f},
298         {0x4d, 0x7f},
299         {0x4e, 0x7f},
300         {0x4f, 0x98},
301         {0x50, 0x98},
302         {0x51, 0x00},
303         {0x52, 0x28},
304         {0x53, 0x70},
305         {0x54, 0x98},
306         {0x58, 0x1a},
307         {0x59, 0x85},
308         {0x5a, 0xa9},
309         {0x5b, 0x64},
310         {0x5c, 0x84},
311         {0x5d, 0x53},
312         {0x5e, 0x0e},
313         {0x5f, 0xf0},
314         {0x60, 0xf0},
315         {0x61, 0xf0},
316         {0x62, 0x00},   /* lcc1 */
317         {0x63, 0x00},   /* lcc2 */
318         {0x64, 0x02},   /* lcc3 */
319         {0x65, 0x16},   /* lcc4 */
320         {0x66, 0x01},   /* lcc5 */
321         {0x69, 0x02},   /* hv */
322         {0x6b, 0x5a},   /* dbvl */
323         {0x6c, 0x04},
324         {0x6d, 0x55},
325         {0x6e, 0x00},
326         {0x6f, 0x9d},
327         {0x70, 0x21},
328         {0x71, 0x78},
329         {0x72, 0x00},
330         {0x73, 0x01},
331         {0x74, 0x3a},
332         {0x75, 0x35},
333         {0x76, 0x01},
334         {0x77, 0x02},
335         {0x7a, 0x12},
336         {0x7b, 0x08},
337         {0x7c, 0x16},
338         {0x7d, 0x30},
339         {0x7e, 0x5e},
340         {0x7f, 0x72},
341         {0x80, 0x82},
342         {0x81, 0x8e},
343         {0x82, 0x9a},
344         {0x83, 0xa4},
345         {0x84, 0xac},
346         {0x85, 0xb8},
347         {0x86, 0xc3},
348         {0x87, 0xd6},
349         {0x88, 0xe6},
350         {0x89, 0xf2},
351         {0x8a, 0x03},
352         {0x8c, 0x89},
353         {0x14, 0x28},   /* com9 */
354         {0x90, 0x7d},
355         {0x91, 0x7b},
356         {0x9d, 0x03},
357         {0x9e, 0x04},
358         {0x9f, 0x7a},
359         {0xa0, 0x79},
360         {0xa1, 0x40},   /* aechm */
361         {0xa4, 0x50},
362         {0xa5, 0x68},   /* com26 */
363         {0xa6, 0x4a},
364         {0xa8, 0xc1},   /* acoma8 */
365         {0xa9, 0xef},   /* acoma9 */
366         {0xaa, 0x92},
367         {0xab, 0x04},
368         {0xac, 0x80},
369         {0xad, 0x80},
370         {0xae, 0x80},
371         {0xaf, 0x80},
372         {0xb2, 0xf2},
373         {0xb3, 0x20},
374         {0xb4, 0x20},
375         {0xb5, 0x00},
376         {0xb6, 0xaf},
377         {0xbb, 0xae},
378         {0xbc, 0x7f},
379         {0xdb, 0x7f},
380         {0xbe, 0x7f},
381         {0xbf, 0x7f},
382         {0xc0, 0xe2},
383         {0xc1, 0xc0},
384         {0xc2, 0x01},
385         {0xc3, 0x4e},
386         {0xc6, 0x85},
387         {0xc7, 0x80},
388         {0xc9, 0xe0},
389         {0xca, 0xe8},
390         {0xcb, 0xf0},
391         {0xcc, 0xd8},
392         {0xcd, 0xf1},
393         {0x4f, 0x98},
394         {0x50, 0x98},
395         {0x51, 0x00},
396         {0x52, 0x28},
397         {0x53, 0x70},
398         {0x54, 0x98},
399         {0x58, 0x1a},
400         {0xff, 0x41},   /* read 41, write ff 00 */
401         {0x41, 0x40},   /* com16 */
402         {0xc5, 0x03},
403         {0x6a, 0x02},
404
405         {0x12, 0x62},   /* com7 - VGA + CIF */
406         {0x36, 0xfa},   /* rsvd36 */
407         {0x69, 0x0a},   /* hv */
408         {0x8c, 0x89},   /* com22 */
409         {0x14, 0x28},   /* com9 */
410         {0x3e, 0x0c},
411         {0x41, 0x40},   /* com16 */
412         {0x72, 0x00},
413         {0x73, 0x00},
414         {0x74, 0x3a},
415         {0x75, 0x35},
416         {0x76, 0x01},
417         {0xc7, 0x80},
418         {0x03, 0x12},   /* vref */
419         {0x17, 0x16},   /* hstart */
420         {0x18, 0x02},   /* hstop */
421         {0x19, 0x01},   /* vstrt */
422         {0x1a, 0x3d},   /* vstop */
423         {0x32, 0xff},   /* href */
424         {0xc0, 0xaa},
425 };
426
427 static const u8 bridge_init_ov965x_2[][2] = {
428         {0x94, 0xaa},
429         {0xf1, 0x60},
430         {0xe5, 0x04},
431         {0xc0, 0x50},
432         {0xc1, 0x3c},
433         {0x8c, 0x00},
434         {0x8d, 0x1c},
435         {0x34, 0x05},
436
437         {0xc2, 0x0c},
438         {0xc3, 0xf9},
439         {0xda, 0x01},
440         {0x50, 0x00},
441         {0x51, 0xa0},
442         {0x52, 0x3c},
443         {0x53, 0x00},
444         {0x54, 0x00},
445         {0x55, 0x00},
446         {0x57, 0x00},
447         {0x5c, 0x00},
448         {0x5a, 0xa0},
449         {0x5b, 0x78},
450         {0x35, 0x02},
451         {0xd9, 0x10},
452         {0x94, 0x11},
453 };
454
455 static const u8 sensor_init_ov965x_2[][2] = {
456         {0x3b, 0xc4},
457         {0x1e, 0x04},   /* mvfp */
458         {0x13, 0xe0},   /* com8 */
459         {0x00, 0x00},   /* gain */
460         {0x13, 0xe7},   /* com8 - everything (AGC, AWB and AEC) */
461         {0x11, 0x03},   /* clkrc */
462         {0x6b, 0x5a},   /* dblv */
463         {0x6a, 0x05},
464         {0xc5, 0x07},
465         {0xa2, 0x4b},
466         {0xa3, 0x3e},
467         {0x2d, 0x00},
468         {0xff, 0x42},   /* read 42, write ff 00 */
469         {0x42, 0xc0},
470         {0x2d, 0x00},
471         {0xff, 0x42},   /* read 42, write ff 00 */
472         {0x42, 0xc1},
473         {0x3f, 0x01},
474         {0xff, 0x42},   /* read 42, write ff 00 */
475         {0x42, 0xc1},
476         {0x4f, 0x98},
477         {0x50, 0x98},
478         {0x51, 0x00},
479         {0x52, 0x28},
480         {0x53, 0x70},
481         {0x54, 0x98},
482         {0x58, 0x1a},
483         {0xff, 0x41},   /* read 41, write ff 00 */
484         {0x41, 0x40},   /* com16 */
485         {0x56, 0x40},
486         {0x55, 0x8f},
487         {0x10, 0x25},   /* aech - exposure high bits */
488         {0xff, 0x13},   /* read 13, write ff 00 */
489         {0x13, 0xe7},   /* com8 - everything (AGC, AWB and AEC) */
490 };
491
492 static const u8 bridge_start_ov965x[][2] = {
493         {0xc2, 0x4c},
494         {0xc3, 0xf9},
495         {0x50, 0x00},
496         {0x51, 0xa0},
497         {0x52, 0x78},
498         {0x53, 0x00},
499         {0x54, 0x00},
500         {0x55, 0x00},
501         {0x57, 0x00},
502         {0x5c, 0x00},
503         {0x5a, 0x28},
504         {0x5b, 0x1e},
505         {0x35, 0x00},
506         {0xd9, 0x21},
507         {0x94, 0x11},
508 };
509
510 static const u8 sensor_start_ov965x[][2] = {
511         {0x3b, 0xe4},
512         {0x1e, 0x04},   /* mvfp */
513         {0x13, 0xe0},   /* com8 */
514         {0x00, 0x00},
515         {0x13, 0xe7},   /* com8 - everything (AGC, AWB and AEC) */
516         {0x11, 0x01},   /* clkrc */
517         {0x6b, 0x5a},   /* dblv */
518         {0x6a, 0x02},
519         {0xc5, 0x03},
520         {0xa2, 0x96},
521         {0xa3, 0x7d},
522         {0xff, 0x13},   /* read 13, write ff 00 */
523         {0x13, 0xe7},
524         {0x3a, 0x80},
525         {0xff, 0x42},   /* read 42, write ff 00 */
526         {0x42, 0xc1},
527 };
528
529
530 static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
531 {
532         struct usb_device *udev = gspca_dev->dev;
533         int ret;
534
535         PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
536         gspca_dev->usb_buf[0] = val;
537         ret = usb_control_msg(udev,
538                               usb_sndctrlpipe(udev, 0),
539                               0x01,
540                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
541                               0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
542         if (ret < 0)
543                 PDEBUG(D_ERR, "write failed");
544 }
545
546 static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
547 {
548         struct usb_device *udev = gspca_dev->dev;
549         int ret;
550
551         ret = usb_control_msg(udev,
552                               usb_rcvctrlpipe(udev, 0),
553                               0x01,
554                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
555                               0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
556         PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, gspca_dev->usb_buf[0]);
557         if (ret < 0)
558                 PDEBUG(D_ERR, "read failed");
559         return gspca_dev->usb_buf[0];
560 }
561
562 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
563  * (direction and output)? */
564 static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
565 {
566         u8 data;
567
568         PDEBUG(D_CONF, "led status: %d", status);
569
570         data = ov534_reg_read(gspca_dev, 0x21);
571         data |= 0x80;
572         ov534_reg_write(gspca_dev, 0x21, data);
573
574         data = ov534_reg_read(gspca_dev, 0x23);
575         if (status)
576                 data |= 0x80;
577         else
578                 data &= ~0x80;
579
580         ov534_reg_write(gspca_dev, 0x23, data);
581
582         if (!status) {
583                 data = ov534_reg_read(gspca_dev, 0x21);
584                 data &= ~0x80;
585                 ov534_reg_write(gspca_dev, 0x21, data);
586         }
587 }
588
589 static int sccb_check_status(struct gspca_dev *gspca_dev)
590 {
591         u8 data;
592         int i;
593
594         for (i = 0; i < 5; i++) {
595                 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
596
597                 switch (data) {
598                 case 0x00:
599                         return 1;
600                 case 0x04:
601                         return 0;
602                 case 0x03:
603                         break;
604                 default:
605                         PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
606                                data, i + 1);
607                 }
608         }
609         return 0;
610 }
611
612 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
613 {
614         PDEBUG(D_USBO, "reg: 0x%02x, val: 0x%02x", reg, val);
615         ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
616         ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
617         ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
618
619         if (!sccb_check_status(gspca_dev))
620                 PDEBUG(D_ERR, "sccb_reg_write failed");
621 }
622
623 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
624 {
625         ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
626         ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
627         if (!sccb_check_status(gspca_dev))
628                 PDEBUG(D_ERR, "sccb_reg_read failed 1");
629
630         ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
631         if (!sccb_check_status(gspca_dev))
632                 PDEBUG(D_ERR, "sccb_reg_read failed 2");
633
634         return ov534_reg_read(gspca_dev, OV534_REG_READ);
635 }
636
637 /* output a bridge sequence (reg - val) */
638 static void reg_w_array(struct gspca_dev *gspca_dev,
639                         const u8 (*data)[2], int len)
640 {
641         while (--len >= 0) {
642                 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
643                 data++;
644         }
645 }
646
647 /* output a sensor sequence (reg - val) */
648 static void sccb_w_array(struct gspca_dev *gspca_dev,
649                         const u8 (*data)[2], int len)
650 {
651         while (--len >= 0) {
652                 if ((*data)[0] != 0xff) {
653                         sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
654                 } else {
655                         sccb_reg_read(gspca_dev, (*data)[1]);
656                         sccb_reg_write(gspca_dev, 0xff, 0x00);
657                 }
658                 data++;
659         }
660 }
661
662 /* set framerate */
663 static void ov534_set_frame_rate(struct gspca_dev *gspca_dev)
664 {
665         struct sd *sd = (struct sd *) gspca_dev;
666         int fr = sd->frame_rate;
667
668         switch (fr) {
669         case 50:
670                 sccb_reg_write(gspca_dev, 0x11, 0x01);
671                 sccb_reg_write(gspca_dev, 0x0d, 0x41);
672                 ov534_reg_write(gspca_dev, 0xe5, 0x02);
673                 break;
674         case 40:
675                 sccb_reg_write(gspca_dev, 0x11, 0x02);
676                 sccb_reg_write(gspca_dev, 0x0d, 0xc1);
677                 ov534_reg_write(gspca_dev, 0xe5, 0x04);
678                 break;
679 /*      case 30: */
680         default:
681                 fr = 30;
682                 sccb_reg_write(gspca_dev, 0x11, 0x04);
683                 sccb_reg_write(gspca_dev, 0x0d, 0x81);
684                 ov534_reg_write(gspca_dev, 0xe5, 0x02);
685                 break;
686         case 15:
687                 sccb_reg_write(gspca_dev, 0x11, 0x03);
688                 sccb_reg_write(gspca_dev, 0x0d, 0x41);
689                 ov534_reg_write(gspca_dev, 0xe5, 0x04);
690                 break;
691         }
692
693         sd->frame_rate = fr;
694         PDEBUG(D_PROBE, "frame_rate: %d", fr);
695 }
696
697 /* this function is called at probe time */
698 static int sd_config(struct gspca_dev *gspca_dev,
699                      const struct usb_device_id *id)
700 {
701         struct sd *sd = (struct sd *) gspca_dev;
702         struct cam *cam;
703
704         sd->sensor = id->driver_info;
705
706         cam = &gspca_dev->cam;
707
708         cam->cam_mode = vga_mode;
709         cam->nmodes = ARRAY_SIZE(vga_mode);
710
711         cam->bulk_size = 16384;
712         cam->bulk_nurbs = 2;
713
714         return 0;
715 }
716
717 /* this function is called at probe and resume time */
718 static int sd_init(struct gspca_dev *gspca_dev)
719 {
720         struct sd *sd = (struct sd *) gspca_dev;
721         u16 sensor_id;
722         static const u8 sensor_addr[2] = {
723                 0x42,                   /* 0 SENSOR_OV772X */
724                 0x60,                   /* 1 SENSOR_OV965X */
725         };
726
727         /* reset bridge */
728         ov534_reg_write(gspca_dev, 0xe7, 0x3a);
729         ov534_reg_write(gspca_dev, 0xe0, 0x08);
730         msleep(100);
731
732         /* initialize the sensor address */
733         ov534_reg_write(gspca_dev, OV534_REG_ADDRESS,
734                                 sensor_addr[sd->sensor]);
735
736         /* reset sensor */
737         sccb_reg_write(gspca_dev, 0x12, 0x80);
738         msleep(10);
739
740         /* probe the sensor */
741         sccb_reg_read(gspca_dev, 0x0a);
742         sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
743         sccb_reg_read(gspca_dev, 0x0b);
744         sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
745         PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
746
747         /* initialize */
748         switch (sd->sensor) {
749         case SENSOR_OV772X:
750                 reg_w_array(gspca_dev, bridge_init_ov722x,
751                                 ARRAY_SIZE(bridge_init_ov722x));
752                 ov534_set_led(gspca_dev, 1);
753                 sccb_w_array(gspca_dev, sensor_init_ov722x,
754                                 ARRAY_SIZE(sensor_init_ov722x));
755                 ov534_reg_write(gspca_dev, 0xe0, 0x09);
756                 ov534_set_led(gspca_dev, 0);
757                 ov534_set_frame_rate(gspca_dev);
758                 break;
759         default:
760 /*      case SENSOR_OV965X: */
761                 reg_w_array(gspca_dev, bridge_init_ov965x,
762                                 ARRAY_SIZE(bridge_init_ov965x));
763                 sccb_w_array(gspca_dev, sensor_init_ov965x,
764                                 ARRAY_SIZE(sensor_init_ov965x));
765                 reg_w_array(gspca_dev, bridge_init_ov965x_2,
766                                 ARRAY_SIZE(bridge_init_ov965x_2));
767                 sccb_w_array(gspca_dev, sensor_init_ov965x_2,
768                                 ARRAY_SIZE(sensor_init_ov965x_2));
769                 ov534_reg_write(gspca_dev, 0xe0, 0x00);
770                 ov534_reg_write(gspca_dev, 0xe0, 0x01);
771                 ov534_set_led(gspca_dev, 0);
772                 ov534_reg_write(gspca_dev, 0xe0, 0x00);
773         }
774
775         return 0;
776 }
777
778 static int sd_start(struct gspca_dev *gspca_dev)
779 {
780         struct sd *sd = (struct sd *) gspca_dev;
781
782         switch (sd->sensor) {
783         case SENSOR_OV772X:
784                 ov534_set_led(gspca_dev, 1);
785                 ov534_reg_write(gspca_dev, 0xe0, 0x00);
786                 break;
787         default:
788 /*      case SENSOR_OV965X: */
789                 reg_w_array(gspca_dev, bridge_start_ov965x,
790                                 ARRAY_SIZE(bridge_start_ov965x));
791                 sccb_w_array(gspca_dev, sensor_start_ov965x,
792                                 ARRAY_SIZE(sensor_start_ov965x));
793                 ov534_reg_write(gspca_dev, 0xe0, 0x00);
794                 ov534_set_led(gspca_dev, 1);
795 /*fixme: other sensor start omitted*/
796         }
797         return 0;
798 }
799
800 static void sd_stopN(struct gspca_dev *gspca_dev)
801 {
802         struct sd *sd = (struct sd *) gspca_dev;
803
804         switch (sd->sensor) {
805         case SENSOR_OV772X:
806                 ov534_reg_write(gspca_dev, 0xe0, 0x09);
807                 ov534_set_led(gspca_dev, 0);
808                 break;
809         default:
810 /*      case SENSOR_OV965X: */
811                 ov534_reg_write(gspca_dev, 0xe0, 0x01);
812                 ov534_set_led(gspca_dev, 0);
813                 ov534_reg_write(gspca_dev, 0xe0, 0x00);
814                 break;
815         }
816 }
817
818 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
819 #define UVC_STREAM_EOH  (1 << 7)
820 #define UVC_STREAM_ERR  (1 << 6)
821 #define UVC_STREAM_STI  (1 << 5)
822 #define UVC_STREAM_RES  (1 << 4)
823 #define UVC_STREAM_SCR  (1 << 3)
824 #define UVC_STREAM_PTS  (1 << 2)
825 #define UVC_STREAM_EOF  (1 << 1)
826 #define UVC_STREAM_FID  (1 << 0)
827
828 static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
829                         __u8 *data, int len)
830 {
831         struct sd *sd = (struct sd *) gspca_dev;
832         __u32 this_pts;
833         u16 this_fid;
834         int remaining_len = len;
835
836         do {
837                 len = min(remaining_len, 2040);         /*fixme: was 2048*/
838
839                 /* Payloads are prefixed with a UVC-style header.  We
840                    consider a frame to start when the FID toggles, or the PTS
841                    changes.  A frame ends when EOF is set, and we've received
842                    the correct number of bytes. */
843
844                 /* Verify UVC header.  Header length is always 12 */
845                 if (data[0] != 12 || len < 12) {
846                         PDEBUG(D_PACK, "bad header");
847                         goto discard;
848                 }
849
850                 /* Check errors */
851                 if (data[1] & UVC_STREAM_ERR) {
852                         PDEBUG(D_PACK, "payload error");
853                         goto discard;
854                 }
855
856                 /* Extract PTS and FID */
857                 if (!(data[1] & UVC_STREAM_PTS)) {
858                         PDEBUG(D_PACK, "PTS not present");
859                         goto discard;
860                 }
861                 this_pts = (data[5] << 24) | (data[4] << 16)
862                                                 | (data[3] << 8) | data[2];
863                 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
864
865                 /* If PTS or FID has changed, start a new frame. */
866                 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
867                         gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
868                                         NULL, 0);
869                         sd->last_pts = this_pts;
870                         sd->last_fid = this_fid;
871                 }
872
873                 /* Add the data from this payload */
874                 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
875                                         data + 12, len - 12);
876
877                 /* If this packet is marked as EOF, end the frame */
878                 if (data[1] & UVC_STREAM_EOF) {
879                         sd->last_pts = 0;
880
881                         if (frame->data_end - frame->data !=
882                             gspca_dev->width * gspca_dev->height * 2) {
883                                 PDEBUG(D_PACK, "short frame");
884                                 goto discard;
885                         }
886
887                         frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
888                                                 NULL, 0);
889                 }
890
891                 /* Done this payload */
892                 goto scan_next;
893
894 discard:
895                 /* Discard data until a new frame starts. */
896                 gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
897
898 scan_next:
899                 remaining_len -= len;
900                 data += len;
901         } while (remaining_len > 0);
902 }
903
904 /* get stream parameters (framerate) */
905 static int sd_get_streamparm(struct gspca_dev *gspca_dev,
906                              struct v4l2_streamparm *parm)
907 {
908         struct v4l2_captureparm *cp = &parm->parm.capture;
909         struct v4l2_fract *tpf = &cp->timeperframe;
910         struct sd *sd = (struct sd *) gspca_dev;
911
912         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
913                 return -EINVAL;
914
915         cp->capability |= V4L2_CAP_TIMEPERFRAME;
916         tpf->numerator = 1;
917         tpf->denominator = sd->frame_rate;
918
919         return 0;
920 }
921
922 /* set stream parameters (framerate) */
923 static int sd_set_streamparm(struct gspca_dev *gspca_dev,
924                              struct v4l2_streamparm *parm)
925 {
926         struct v4l2_captureparm *cp = &parm->parm.capture;
927         struct v4l2_fract *tpf = &cp->timeperframe;
928         struct sd *sd = (struct sd *) gspca_dev;
929
930         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
931                 return -EINVAL;
932
933         /* Set requested framerate */
934         sd->frame_rate = tpf->denominator / tpf->numerator;
935         ov534_set_frame_rate(gspca_dev);
936
937         /* Return the actual framerate */
938         tpf->numerator = 1;
939         tpf->denominator = sd->frame_rate;
940
941         return 0;
942 }
943
944 /* sub-driver description */
945 static const struct sd_desc sd_desc = {
946         .name     = MODULE_NAME,
947         .ctrls    = sd_ctrls,
948         .nctrls   = ARRAY_SIZE(sd_ctrls),
949         .config   = sd_config,
950         .init     = sd_init,
951         .start    = sd_start,
952         .stopN    = sd_stopN,
953         .pkt_scan = sd_pkt_scan,
954         .get_streamparm = sd_get_streamparm,
955         .set_streamparm = sd_set_streamparm,
956 };
957
958 /* -- module initialisation -- */
959 static const __devinitdata struct usb_device_id device_table[] = {
960         {USB_DEVICE(0x06f8, 0x3003), .driver_info = SENSOR_OV965X},
961         {USB_DEVICE(0x1415, 0x2000), .driver_info = SENSOR_OV772X},
962         {}
963 };
964
965 MODULE_DEVICE_TABLE(usb, device_table);
966
967 /* -- device connect -- */
968 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
969 {
970         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
971                                THIS_MODULE);
972 }
973
974 static struct usb_driver sd_driver = {
975         .name       = MODULE_NAME,
976         .id_table   = device_table,
977         .probe      = sd_probe,
978         .disconnect = gspca_disconnect,
979 #ifdef CONFIG_PM
980         .suspend    = gspca_suspend,
981         .resume     = gspca_resume,
982 #endif
983 };
984
985 /* -- module insert / remove -- */
986 static int __init sd_mod_init(void)
987 {
988         int ret;
989         ret = usb_register(&sd_driver);
990         if (ret < 0)
991                 return ret;
992         PDEBUG(D_PROBE, "registered");
993         return 0;
994 }
995
996 static void __exit sd_mod_exit(void)
997 {
998         usb_deregister(&sd_driver);
999         PDEBUG(D_PROBE, "deregistered");
1000 }
1001
1002 module_init(sd_mod_init);
1003 module_exit(sd_mod_exit);