2a0e8a3e794fe040cf099b189ca0777d8aeaab58
[pandora-kernel.git] / drivers / media / video / gspca / ov534.c
1 /*
2  * ov534-ov772x gspca driver
3  *
4  * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
5  * Copyright (C) 2008 Jim Paris <jim@jtan.com>
6  * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
7  *
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  *
12  * PS3 Eye camera enhanced by Richard Kaswy http://kaswy.free.fr
13  * PS3 Eye camera, brightness, contrast, hue, AWB control added
14  *      by Max Thrun <bear24rw@gmail.com>
15  *
16  * 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
31 #define MODULE_NAME "ov534"
32
33 #include "gspca.h"
34
35 #define OV534_REG_ADDRESS       0xf1    /* sensor address */
36 #define OV534_REG_SUBADDR       0xf2
37 #define OV534_REG_WRITE         0xf3
38 #define OV534_REG_READ          0xf4
39 #define OV534_REG_OPERATION     0xf5
40 #define OV534_REG_STATUS        0xf6
41
42 #define OV534_OP_WRITE_3        0x37
43 #define OV534_OP_WRITE_2        0x33
44 #define OV534_OP_READ_2         0xf9
45
46 #define CTRL_TIMEOUT 500
47
48 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
49 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
50 MODULE_LICENSE("GPL");
51
52 /* specific webcam descriptor */
53 struct sd {
54         struct gspca_dev gspca_dev;     /* !! must be the first item */
55         __u32 last_pts;
56         u16 last_fid;
57         u8 frame_rate;
58
59         u8 brightness;
60         u8 contrast;
61         u8 gain;
62         u8 exposure;
63         u8 hue;
64         u8 autogain;
65         u8 awb;
66         s8 sharpness;
67         u8 hflip;
68         u8 vflip;
69
70 };
71
72 /* V4L2 controls supported by the driver */
73 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
74 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
75 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
76 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
77 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
78 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
79 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
80 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
81 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
82 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
83 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
84 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
85 static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val);
86 static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val);
87 static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val);
88 static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val);
89 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
90 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
91 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
92 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
93
94 static const struct ctrl sd_ctrls[] = {
95     {                                                   /* 0 */
96         {
97                 .id      = V4L2_CID_BRIGHTNESS,
98                 .type    = V4L2_CTRL_TYPE_INTEGER,
99                 .name    = "Brightness",
100                 .minimum = 0,
101                 .maximum = 255,
102                 .step    = 1,
103 #define BRIGHTNESS_DEF 20
104                 .default_value = BRIGHTNESS_DEF,
105         },
106         .set = sd_setbrightness,
107         .get = sd_getbrightness,
108     },
109     {                                                   /* 1 */
110         {
111                 .id      = V4L2_CID_CONTRAST,
112                 .type    = V4L2_CTRL_TYPE_INTEGER,
113                 .name    = "Contrast",
114                 .minimum = 0,
115                 .maximum = 255,
116                 .step    = 1,
117 #define CONTRAST_DEF 37
118                 .default_value = CONTRAST_DEF,
119         },
120         .set = sd_setcontrast,
121         .get = sd_getcontrast,
122     },
123     {                                                   /* 2 */
124         {
125             .id      = V4L2_CID_GAIN,
126             .type    = V4L2_CTRL_TYPE_INTEGER,
127             .name    = "Main Gain",
128             .minimum = 0,
129             .maximum = 63,
130             .step    = 1,
131 #define GAIN_DEF 20
132             .default_value = GAIN_DEF,
133         },
134         .set = sd_setgain,
135         .get = sd_getgain,
136     },
137     {                                                   /* 3 */
138         {
139             .id      = V4L2_CID_EXPOSURE,
140             .type    = V4L2_CTRL_TYPE_INTEGER,
141             .name    = "Exposure",
142             .minimum = 0,
143             .maximum = 255,
144             .step    = 1,
145 #define EXPO_DEF 120
146             .default_value = EXPO_DEF,
147         },
148         .set = sd_setexposure,
149         .get = sd_getexposure,
150     },
151     {                                                   /* 4 */
152         {
153                 .id      = V4L2_CID_HUE,
154                 .type    = V4L2_CTRL_TYPE_INTEGER,
155                 .name    = "Hue",
156                 .minimum = 0,
157                 .maximum = 255,
158                 .step    = 1,
159 #define HUE_DEF 143
160                 .default_value = HUE_DEF,
161         },
162         .set = sd_sethue,
163         .get = sd_gethue,
164     },
165     {                                                   /* 5 */
166         {
167             .id      = V4L2_CID_AUTOGAIN,
168             .type    = V4L2_CTRL_TYPE_BOOLEAN,
169             .name    = "Autogain",
170             .minimum = 0,
171             .maximum = 1,
172             .step    = 1,
173 #define AUTOGAIN_DEF 0
174             .default_value = AUTOGAIN_DEF,
175         },
176         .set = sd_setautogain,
177         .get = sd_getautogain,
178     },
179 #define AWB_IDX 6
180     {                                                   /* 6 */
181         {
182                 .id      = V4L2_CID_AUTO_WHITE_BALANCE,
183                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
184                 .name    = "Auto White Balance",
185                 .minimum = 0,
186                 .maximum = 1,
187                 .step    = 1,
188 #define AWB_DEF 0
189                 .default_value = AWB_DEF,
190         },
191         .set = sd_setawb,
192         .get = sd_getawb,
193     },
194     {                                                   /* 7 */
195         {
196             .id      = V4L2_CID_SHARPNESS,
197             .type    = V4L2_CTRL_TYPE_INTEGER,
198             .name    = "Sharpness",
199             .minimum = 0,
200             .maximum = 63,
201             .step    = 1,
202 #define SHARPNESS_DEF 0
203             .default_value = SHARPNESS_DEF,
204         },
205         .set = sd_setsharpness,
206         .get = sd_getsharpness,
207     },
208     {                                                   /* 8 */
209         {
210             .id      = V4L2_CID_HFLIP,
211             .type    = V4L2_CTRL_TYPE_BOOLEAN,
212             .name    = "HFlip",
213             .minimum = 0,
214             .maximum = 1,
215             .step    = 1,
216 #define HFLIP_DEF 0
217             .default_value = HFLIP_DEF,
218         },
219         .set = sd_sethflip,
220         .get = sd_gethflip,
221     },
222     {                                                   /* 9 */
223         {
224             .id      = V4L2_CID_VFLIP,
225             .type    = V4L2_CTRL_TYPE_BOOLEAN,
226             .name    = "VFlip",
227             .minimum = 0,
228             .maximum = 1,
229             .step    = 1,
230 #define VFLIP_DEF 0
231             .default_value = VFLIP_DEF,
232         },
233         .set = sd_setvflip,
234         .get = sd_getvflip,
235     },
236 };
237
238 static const struct v4l2_pix_format ov772x_mode[] = {
239         {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
240          .bytesperline = 320 * 2,
241          .sizeimage = 320 * 240 * 2,
242          .colorspace = V4L2_COLORSPACE_SRGB,
243          .priv = 1},
244         {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
245          .bytesperline = 640 * 2,
246          .sizeimage = 640 * 480 * 2,
247          .colorspace = V4L2_COLORSPACE_SRGB,
248          .priv = 0},
249 };
250
251 static const u8 qvga_rates[] = {125, 100, 75, 60, 50, 40, 30};
252 static const u8 vga_rates[] = {60, 50, 40, 30, 15};
253
254 static const struct framerates ov772x_framerates[] = {
255         { /* 320x240 */
256                 .rates = qvga_rates,
257                 .nrates = ARRAY_SIZE(qvga_rates),
258         },
259         { /* 640x480 */
260                 .rates = vga_rates,
261                 .nrates = ARRAY_SIZE(vga_rates),
262         },
263 };
264
265 static const u8 bridge_init[][2] = {
266         { 0xc2, 0x0c },
267         { 0x88, 0xf8 },
268         { 0xc3, 0x69 },
269         { 0x89, 0xff },
270         { 0x76, 0x03 },
271         { 0x92, 0x01 },
272         { 0x93, 0x18 },
273         { 0x94, 0x10 },
274         { 0x95, 0x10 },
275         { 0xe2, 0x00 },
276         { 0xe7, 0x3e },
277
278         { 0x96, 0x00 },
279
280         { 0x97, 0x20 },
281         { 0x97, 0x20 },
282         { 0x97, 0x20 },
283         { 0x97, 0x0a },
284         { 0x97, 0x3f },
285         { 0x97, 0x4a },
286         { 0x97, 0x20 },
287         { 0x97, 0x15 },
288         { 0x97, 0x0b },
289
290         { 0x8e, 0x40 },
291         { 0x1f, 0x81 },
292         { 0x34, 0x05 },
293         { 0xe3, 0x04 },
294         { 0x88, 0x00 },
295         { 0x89, 0x00 },
296         { 0x76, 0x00 },
297         { 0xe7, 0x2e },
298         { 0x31, 0xf9 },
299         { 0x25, 0x42 },
300         { 0x21, 0xf0 },
301
302         { 0x1c, 0x00 },
303         { 0x1d, 0x40 },
304         { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
305         { 0x1d, 0x00 }, /* payload size */
306
307         { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
308         { 0x1d, 0x58 }, /* frame size */
309         { 0x1d, 0x00 }, /* frame size */
310
311         { 0x1c, 0x0a },
312         { 0x1d, 0x08 }, /* turn on UVC header */
313         { 0x1d, 0x0e }, /* .. */
314
315         { 0x8d, 0x1c },
316         { 0x8e, 0x80 },
317         { 0xe5, 0x04 },
318
319         { 0xc0, 0x50 },
320         { 0xc1, 0x3c },
321         { 0xc2, 0x0c },
322 };
323 static const u8 sensor_init[][2] = {
324         { 0x12, 0x80 },
325         { 0x11, 0x01 },
326 /*fixme: better have a delay?*/
327         { 0x11, 0x01 },
328         { 0x11, 0x01 },
329         { 0x11, 0x01 },
330         { 0x11, 0x01 },
331         { 0x11, 0x01 },
332         { 0x11, 0x01 },
333         { 0x11, 0x01 },
334         { 0x11, 0x01 },
335         { 0x11, 0x01 },
336         { 0x11, 0x01 },
337
338         { 0x3d, 0x03 },
339         { 0x17, 0x26 },
340         { 0x18, 0xa0 },
341         { 0x19, 0x07 },
342         { 0x1a, 0xf0 },
343         { 0x32, 0x00 },
344         { 0x29, 0xa0 },
345         { 0x2c, 0xf0 },
346         { 0x65, 0x20 },
347         { 0x11, 0x01 },
348         { 0x42, 0x7f },
349         { 0x63, 0xaa },         /* AWB - was e0 */
350         { 0x64, 0xff },
351         { 0x66, 0x00 },
352         { 0x13, 0xf0 },         /* com8 */
353         { 0x0d, 0x41 },
354         { 0x0f, 0xc5 },
355         { 0x14, 0x11 },
356
357         { 0x22, 0x7f },
358         { 0x23, 0x03 },
359         { 0x24, 0x40 },
360         { 0x25, 0x30 },
361         { 0x26, 0xa1 },
362         { 0x2a, 0x00 },
363         { 0x2b, 0x00 },
364         { 0x6b, 0xaa },
365         { 0x13, 0xff },         /* AWB */
366
367         { 0x90, 0x05 },
368         { 0x91, 0x01 },
369         { 0x92, 0x03 },
370         { 0x93, 0x00 },
371         { 0x94, 0x60 },
372         { 0x95, 0x3c },
373         { 0x96, 0x24 },
374         { 0x97, 0x1e },
375         { 0x98, 0x62 },
376         { 0x99, 0x80 },
377         { 0x9a, 0x1e },
378         { 0x9b, 0x08 },
379         { 0x9c, 0x20 },
380         { 0x9e, 0x81 },
381
382         { 0xa6, 0x04 },
383         { 0x7e, 0x0c },
384         { 0x7f, 0x16 },
385         { 0x80, 0x2a },
386         { 0x81, 0x4e },
387         { 0x82, 0x61 },
388         { 0x83, 0x6f },
389         { 0x84, 0x7b },
390         { 0x85, 0x86 },
391         { 0x86, 0x8e },
392         { 0x87, 0x97 },
393         { 0x88, 0xa4 },
394         { 0x89, 0xaf },
395         { 0x8a, 0xc5 },
396         { 0x8b, 0xd7 },
397         { 0x8c, 0xe8 },
398         { 0x8d, 0x20 },
399
400         { 0x0c, 0x90 },
401
402         { 0x2b, 0x00 },
403         { 0x22, 0x7f },
404         { 0x23, 0x03 },
405         { 0x11, 0x01 },
406         { 0x0c, 0xd0 },
407         { 0x64, 0xff },
408         { 0x0d, 0x41 },
409
410         { 0x14, 0x41 },
411         { 0x0e, 0xcd },
412         { 0xac, 0xbf },
413         { 0x8e, 0x00 },         /* De-noise threshold */
414         { 0x0c, 0xd0 }
415 };
416 static const u8 bridge_start_vga[][2] = {
417         {0x1c, 0x00},
418         {0x1d, 0x40},
419         {0x1d, 0x02},
420         {0x1d, 0x00},
421         {0x1d, 0x02},
422         {0x1d, 0x58},
423         {0x1d, 0x00},
424         {0xc0, 0x50},
425         {0xc1, 0x3c},
426 };
427 static const u8 sensor_start_vga[][2] = {
428         {0x12, 0x00},
429         {0x17, 0x26},
430         {0x18, 0xa0},
431         {0x19, 0x07},
432         {0x1a, 0xf0},
433         {0x29, 0xa0},
434         {0x2c, 0xf0},
435         {0x65, 0x20},
436 };
437 static const u8 bridge_start_qvga[][2] = {
438         {0x1c, 0x00},
439         {0x1d, 0x40},
440         {0x1d, 0x02},
441         {0x1d, 0x00},
442         {0x1d, 0x01},
443         {0x1d, 0x4b},
444         {0x1d, 0x00},
445         {0xc0, 0x28},
446         {0xc1, 0x1e},
447 };
448 static const u8 sensor_start_qvga[][2] = {
449         {0x12, 0x40},
450         {0x17, 0x3f},
451         {0x18, 0x50},
452         {0x19, 0x03},
453         {0x1a, 0x78},
454         {0x29, 0x50},
455         {0x2c, 0x78},
456         {0x65, 0x2f},
457 };
458
459 static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
460 {
461         struct usb_device *udev = gspca_dev->dev;
462         int ret;
463
464         PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
465         gspca_dev->usb_buf[0] = val;
466         ret = usb_control_msg(udev,
467                               usb_sndctrlpipe(udev, 0),
468                               0x01,
469                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
470                               0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
471         if (ret < 0)
472                 PDEBUG(D_ERR, "write failed");
473 }
474
475 static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
476 {
477         struct usb_device *udev = gspca_dev->dev;
478         int ret;
479
480         ret = usb_control_msg(udev,
481                               usb_rcvctrlpipe(udev, 0),
482                               0x01,
483                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
484                               0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
485         PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, gspca_dev->usb_buf[0]);
486         if (ret < 0)
487                 PDEBUG(D_ERR, "read failed");
488         return gspca_dev->usb_buf[0];
489 }
490
491 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
492  * (direction and output)? */
493 static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
494 {
495         u8 data;
496
497         PDEBUG(D_CONF, "led status: %d", status);
498
499         data = ov534_reg_read(gspca_dev, 0x21);
500         data |= 0x80;
501         ov534_reg_write(gspca_dev, 0x21, data);
502
503         data = ov534_reg_read(gspca_dev, 0x23);
504         if (status)
505                 data |= 0x80;
506         else
507                 data &= ~0x80;
508
509         ov534_reg_write(gspca_dev, 0x23, data);
510
511         if (!status) {
512                 data = ov534_reg_read(gspca_dev, 0x21);
513                 data &= ~0x80;
514                 ov534_reg_write(gspca_dev, 0x21, data);
515         }
516 }
517
518 static int sccb_check_status(struct gspca_dev *gspca_dev)
519 {
520         u8 data;
521         int i;
522
523         for (i = 0; i < 5; i++) {
524                 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
525
526                 switch (data) {
527                 case 0x00:
528                         return 1;
529                 case 0x04:
530                         return 0;
531                 case 0x03:
532                         break;
533                 default:
534                         PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
535                                data, i + 1);
536                 }
537         }
538         return 0;
539 }
540
541 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
542 {
543         PDEBUG(D_USBO, "reg: 0x%02x, val: 0x%02x", reg, val);
544         ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
545         ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
546         ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
547
548         if (!sccb_check_status(gspca_dev))
549                 PDEBUG(D_ERR, "sccb_reg_write failed");
550 }
551
552 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
553 {
554         ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
555         ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
556         if (!sccb_check_status(gspca_dev))
557                 PDEBUG(D_ERR, "sccb_reg_read failed 1");
558
559         ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
560         if (!sccb_check_status(gspca_dev))
561                 PDEBUG(D_ERR, "sccb_reg_read failed 2");
562
563         return ov534_reg_read(gspca_dev, OV534_REG_READ);
564 }
565
566 /* output a bridge sequence (reg - val) */
567 static void reg_w_array(struct gspca_dev *gspca_dev,
568                         const u8 (*data)[2], int len)
569 {
570         while (--len >= 0) {
571                 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
572                 data++;
573         }
574 }
575
576 /* output a sensor sequence (reg - val) */
577 static void sccb_w_array(struct gspca_dev *gspca_dev,
578                         const u8 (*data)[2], int len)
579 {
580         while (--len >= 0) {
581                 if ((*data)[0] != 0xff) {
582                         sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
583                 } else {
584                         sccb_reg_read(gspca_dev, (*data)[1]);
585                         sccb_reg_write(gspca_dev, 0xff, 0x00);
586                 }
587                 data++;
588         }
589 }
590
591 /* ov772x specific controls */
592 static void set_frame_rate(struct gspca_dev *gspca_dev)
593 {
594         struct sd *sd = (struct sd *) gspca_dev;
595         int i;
596         struct rate_s {
597                 u8 fps;
598                 u8 r11;
599                 u8 r0d;
600                 u8 re5;
601         };
602         const struct rate_s *r;
603         static const struct rate_s rate_0[] = { /* 640x480 */
604                 {60, 0x01, 0xc1, 0x04},
605                 {50, 0x01, 0x41, 0x02},
606                 {40, 0x02, 0xc1, 0x04},
607                 {30, 0x04, 0x81, 0x02},
608                 {15, 0x03, 0x41, 0x04},
609         };
610         static const struct rate_s rate_1[] = { /* 320x240 */
611                 {125, 0x02, 0x81, 0x02},
612                 {100, 0x02, 0xc1, 0x04},
613                 {75, 0x03, 0xc1, 0x04},
614                 {60, 0x04, 0xc1, 0x04},
615                 {50, 0x02, 0x41, 0x04},
616                 {40, 0x03, 0x41, 0x04},
617                 {30, 0x04, 0x41, 0x04},
618         };
619
620         if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
621                 r = rate_0;
622                 i = ARRAY_SIZE(rate_0);
623         } else {
624                 r = rate_1;
625                 i = ARRAY_SIZE(rate_1);
626         }
627         while (--i > 0) {
628                 if (sd->frame_rate >= r->fps)
629                         break;
630                 r++;
631         }
632
633         sccb_reg_write(gspca_dev, 0x11, r->r11);
634         sccb_reg_write(gspca_dev, 0x0d, r->r0d);
635         ov534_reg_write(gspca_dev, 0xe5, r->re5);
636
637         PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
638 }
639
640 static void setbrightness(struct gspca_dev *gspca_dev)
641 {
642         struct sd *sd = (struct sd *) gspca_dev;
643
644         sccb_reg_write(gspca_dev, 0x9B, sd->brightness);
645 }
646
647 static void setcontrast(struct gspca_dev *gspca_dev)
648 {
649         struct sd *sd = (struct sd *) gspca_dev;
650
651         sccb_reg_write(gspca_dev, 0x9C, sd->contrast);
652 }
653
654 static void setgain(struct gspca_dev *gspca_dev)
655 {
656         struct sd *sd = (struct sd *) gspca_dev;
657         u8 val;
658
659         val = sd->gain;
660         switch (val & 0x30) {
661         case 0x00:
662                 val &= 0x0f;
663                 break;
664         case 0x10:
665                 val &= 0x0f;
666                 val |= 0x30;
667                 break;
668         case 0x20:
669                 val &= 0x0f;
670                 val |= 0x70;
671                 break;
672         default:
673 /*      case 0x30: */
674                 val &= 0x0f;
675                 val |= 0xf0;
676                 break;
677         }
678         sccb_reg_write(gspca_dev, 0x00, val);
679 }
680
681 static void setexposure(struct gspca_dev *gspca_dev)
682 {
683         struct sd *sd = (struct sd *) gspca_dev;
684         u8 val;
685
686         val = sd->exposure;
687         sccb_reg_write(gspca_dev, 0x08, val >> 7);
688         sccb_reg_write(gspca_dev, 0x10, val << 1);
689 }
690
691 static void sethue(struct gspca_dev *gspca_dev)
692 {
693         struct sd *sd = (struct sd *) gspca_dev;
694
695         sccb_reg_write(gspca_dev, 0x01, sd->hue);
696 }
697
698 static void setautogain(struct gspca_dev *gspca_dev)
699 {
700         struct sd *sd = (struct sd *) gspca_dev;
701
702         if (sd->autogain) {
703                 sccb_reg_write(gspca_dev, 0x13, 0xf7); /* AGC,AEC,AWB ON */
704                 sccb_reg_write(gspca_dev, 0x64,
705                                 sccb_reg_read(gspca_dev, 0x64) | 0x03);
706         } else {
707                 sccb_reg_write(gspca_dev, 0x13, 0xf0); /* AGC,AEC,AWB OFF */
708                 sccb_reg_write(gspca_dev, 0x64,
709                                 sccb_reg_read(gspca_dev, 0x64) & 0xfc);
710         }
711 }
712
713 static void setawb(struct gspca_dev *gspca_dev)
714 {
715         struct sd *sd = (struct sd *) gspca_dev;
716
717         if (sd->awb)
718                 sccb_reg_write(gspca_dev, 0x63, 0xe0);  /* AWB on */
719         else
720                 sccb_reg_write(gspca_dev, 0x63, 0xaa);  /* AWB off */
721 }
722
723 static void setsharpness(struct gspca_dev *gspca_dev)
724 {
725         struct sd *sd = (struct sd *) gspca_dev;
726         u8 val;
727
728         val = sd->sharpness;
729         sccb_reg_write(gspca_dev, 0x91, val);   /* vga noise */
730         sccb_reg_write(gspca_dev, 0x8e, val);   /* qvga noise */
731 }
732
733 static void sethflip(struct gspca_dev *gspca_dev)
734 {
735         struct sd *sd = (struct sd *) gspca_dev;
736
737         if (sd->hflip == 0)
738                 sccb_reg_write(gspca_dev, 0x0c,
739                                 sccb_reg_read(gspca_dev, 0x0c) | 0x40);
740         else
741                 sccb_reg_write(gspca_dev, 0x0c,
742                                 sccb_reg_read(gspca_dev, 0x0c) & 0xbf);
743 }
744
745 static void setvflip(struct gspca_dev *gspca_dev)
746 {
747         struct sd *sd = (struct sd *) gspca_dev;
748
749         if (sd->vflip == 0)
750                 sccb_reg_write(gspca_dev, 0x0c,
751                                 sccb_reg_read(gspca_dev, 0x0c) | 0x80);
752         else
753                 sccb_reg_write(gspca_dev, 0x0c,
754                                 sccb_reg_read(gspca_dev, 0x0c) & 0x7f);
755 }
756
757 /* this function is called at probe time */
758 static int sd_config(struct gspca_dev *gspca_dev,
759                      const struct usb_device_id *id)
760 {
761         struct sd *sd = (struct sd *) gspca_dev;
762         struct cam *cam;
763
764         cam = &gspca_dev->cam;
765
766         cam->cam_mode = ov772x_mode;
767         cam->nmodes = ARRAY_SIZE(ov772x_mode);
768         cam->mode_framerates = ov772x_framerates;
769
770         cam->bulk = 1;
771         cam->bulk_size = 16384;
772         cam->bulk_nurbs = 2;
773
774         sd->frame_rate = 30;
775
776         sd->brightness = BRIGHTNESS_DEF;
777         sd->contrast = CONTRAST_DEF;
778         sd->gain = GAIN_DEF;
779         sd->exposure = EXPO_DEF;
780         sd->hue = HUE_DEF;
781 #if AUTOGAIN_DEF != 0
782         sd->autogain = AUTOGAIN_DEF;
783 #else
784         gspca_dev->ctrl_inac |= (1 << AWB_IDX);
785 #endif
786 #if AWB_DEF != 0
787         sd->awb = AWB_DEF
788 #endif
789 #if SHARPNESS_DEF != 0
790         sd->sharpness = SHARPNESS_DEF;
791 #endif
792 #if HFLIP_DEF != 0
793         sd->hflip = HFLIP_DEF;
794 #endif
795 #if VFLIP_DEF != 0
796         sd->vflip = VFLIP_DEF;
797 #endif
798
799         return 0;
800 }
801
802 /* this function is called at probe and resume time */
803 static int sd_init(struct gspca_dev *gspca_dev)
804 {
805         u16 sensor_id;
806
807         /* reset bridge */
808         ov534_reg_write(gspca_dev, 0xe7, 0x3a);
809         ov534_reg_write(gspca_dev, 0xe0, 0x08);
810         msleep(100);
811
812         /* initialize the sensor address */
813         ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
814
815         /* reset sensor */
816         sccb_reg_write(gspca_dev, 0x12, 0x80);
817         msleep(10);
818
819         /* probe the sensor */
820         sccb_reg_read(gspca_dev, 0x0a);
821         sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
822         sccb_reg_read(gspca_dev, 0x0b);
823         sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
824         PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
825
826         /* initialize */
827         reg_w_array(gspca_dev, bridge_init,
828                         ARRAY_SIZE(bridge_init));
829         ov534_set_led(gspca_dev, 1);
830         sccb_w_array(gspca_dev, sensor_init,
831                         ARRAY_SIZE(sensor_init));
832         ov534_reg_write(gspca_dev, 0xe0, 0x09);
833         ov534_set_led(gspca_dev, 0);
834         set_frame_rate(gspca_dev);
835
836         return 0;
837 }
838
839 static int sd_start(struct gspca_dev *gspca_dev)
840 {
841         int mode;
842
843         mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
844         if (mode != 0) {        /* 320x240 */
845                 reg_w_array(gspca_dev, bridge_start_qvga,
846                                 ARRAY_SIZE(bridge_start_qvga));
847                 sccb_w_array(gspca_dev, sensor_start_qvga,
848                                 ARRAY_SIZE(sensor_start_qvga));
849         } else {                /* 640x480 */
850                 reg_w_array(gspca_dev, bridge_start_vga,
851                                 ARRAY_SIZE(bridge_start_vga));
852                 sccb_w_array(gspca_dev, sensor_start_vga,
853                                 ARRAY_SIZE(sensor_start_vga));
854         }
855         set_frame_rate(gspca_dev);
856
857         setautogain(gspca_dev);
858         setawb(gspca_dev);
859         setgain(gspca_dev);
860         sethue(gspca_dev);
861         setexposure(gspca_dev);
862         setbrightness(gspca_dev);
863         setcontrast(gspca_dev);
864         setsharpness(gspca_dev);
865         setvflip(gspca_dev);
866         sethflip(gspca_dev);
867
868         ov534_set_led(gspca_dev, 1);
869         ov534_reg_write(gspca_dev, 0xe0, 0x00);
870         return 0;
871 }
872
873 static void sd_stopN(struct gspca_dev *gspca_dev)
874 {
875         ov534_reg_write(gspca_dev, 0xe0, 0x09);
876         ov534_set_led(gspca_dev, 0);
877 }
878
879 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
880 #define UVC_STREAM_EOH  (1 << 7)
881 #define UVC_STREAM_ERR  (1 << 6)
882 #define UVC_STREAM_STI  (1 << 5)
883 #define UVC_STREAM_RES  (1 << 4)
884 #define UVC_STREAM_SCR  (1 << 3)
885 #define UVC_STREAM_PTS  (1 << 2)
886 #define UVC_STREAM_EOF  (1 << 1)
887 #define UVC_STREAM_FID  (1 << 0)
888
889 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
890                         u8 *data, int len)
891 {
892         struct sd *sd = (struct sd *) gspca_dev;
893         __u32 this_pts;
894         u16 this_fid;
895         int remaining_len = len;
896
897         do {
898                 len = min(remaining_len, 2048);
899
900                 /* Payloads are prefixed with a UVC-style header.  We
901                    consider a frame to start when the FID toggles, or the PTS
902                    changes.  A frame ends when EOF is set, and we've received
903                    the correct number of bytes. */
904
905                 /* Verify UVC header.  Header length is always 12 */
906                 if (data[0] != 12 || len < 12) {
907                         PDEBUG(D_PACK, "bad header");
908                         goto discard;
909                 }
910
911                 /* Check errors */
912                 if (data[1] & UVC_STREAM_ERR) {
913                         PDEBUG(D_PACK, "payload error");
914                         goto discard;
915                 }
916
917                 /* Extract PTS and FID */
918                 if (!(data[1] & UVC_STREAM_PTS)) {
919                         PDEBUG(D_PACK, "PTS not present");
920                         goto discard;
921                 }
922                 this_pts = (data[5] << 24) | (data[4] << 16)
923                                                 | (data[3] << 8) | data[2];
924                 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
925
926                 /* If PTS or FID has changed, start a new frame. */
927                 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
928                         if (gspca_dev->last_packet_type == INTER_PACKET)
929                                 gspca_frame_add(gspca_dev, LAST_PACKET,
930                                                 NULL, 0);
931                         sd->last_pts = this_pts;
932                         sd->last_fid = this_fid;
933                         gspca_frame_add(gspca_dev, FIRST_PACKET,
934                                         data + 12, len - 12);
935                 /* If this packet is marked as EOF, end the frame */
936                 } else if (data[1] & UVC_STREAM_EOF) {
937                         struct gspca_frame *frame;
938
939                         sd->last_pts = 0;
940                         frame = gspca_get_i_frame(gspca_dev);
941                         if (frame == NULL)
942                                 goto discard;
943                         if (frame->data_end - frame->data + (len - 12) !=
944                             gspca_dev->width * gspca_dev->height * 2) {
945                                 PDEBUG(D_PACK, "wrong sized frame");
946                                 goto discard;
947                         }
948                         gspca_frame_add(gspca_dev, LAST_PACKET,
949                                         data + 12, len - 12);
950                 } else {
951
952                         /* Add the data from this payload */
953                         gspca_frame_add(gspca_dev, INTER_PACKET,
954                                         data + 12, len - 12);
955                 }
956
957                 /* Done this payload */
958                 goto scan_next;
959
960 discard:
961                 /* Discard data until a new frame starts. */
962                 gspca_dev->last_packet_type = DISCARD_PACKET;
963
964 scan_next:
965                 remaining_len -= len;
966                 data += len;
967         } while (remaining_len > 0);
968 }
969
970 /* controls */
971 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
972 {
973         struct sd *sd = (struct sd *) gspca_dev;
974
975         sd->gain = val;
976         if (gspca_dev->streaming)
977                 setgain(gspca_dev);
978         return 0;
979 }
980
981 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
982 {
983         struct sd *sd = (struct sd *) gspca_dev;
984
985         *val = sd->gain;
986         return 0;
987 }
988
989 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
990 {
991         struct sd *sd = (struct sd *) gspca_dev;
992
993         sd->exposure = val;
994         if (gspca_dev->streaming)
995                 setexposure(gspca_dev);
996         return 0;
997 }
998
999 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1000 {
1001         struct sd *sd = (struct sd *) gspca_dev;
1002
1003         *val = sd->exposure;
1004         return 0;
1005 }
1006
1007 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
1008 {
1009         struct sd *sd = (struct sd *) gspca_dev;
1010
1011         sd->brightness = val;
1012         if (gspca_dev->streaming)
1013                 setbrightness(gspca_dev);
1014         return 0;
1015 }
1016
1017 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1018 {
1019         struct sd *sd = (struct sd *) gspca_dev;
1020
1021         *val = sd->brightness;
1022         return 0;
1023 }
1024
1025 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
1026 {
1027         struct sd *sd = (struct sd *) gspca_dev;
1028
1029         sd->contrast = val;
1030         if (gspca_dev->streaming)
1031                 setcontrast(gspca_dev);
1032         return 0;
1033 }
1034
1035 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
1036 {
1037         struct sd *sd = (struct sd *) gspca_dev;
1038
1039         *val = sd->contrast;
1040         return 0;
1041 }
1042
1043 static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val)
1044 {
1045         struct sd *sd = (struct sd *) gspca_dev;
1046
1047         sd->hue = val;
1048         if (gspca_dev->streaming)
1049                 sethue(gspca_dev);
1050         return 0;
1051 }
1052
1053 static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val)
1054 {
1055         struct sd *sd = (struct sd *) gspca_dev;
1056
1057         *val = sd->hue;
1058         return 0;
1059 }
1060
1061 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1062 {
1063         struct sd *sd = (struct sd *) gspca_dev;
1064
1065         sd->autogain = val;
1066
1067         if (gspca_dev->streaming) {
1068
1069                 /* the auto white balance control works only
1070                  * when auto gain is set */
1071                 if (val)
1072                         gspca_dev->ctrl_inac &= ~(1 << AWB_IDX);
1073                 else
1074                         gspca_dev->ctrl_inac |= (1 << AWB_IDX);
1075                 setautogain(gspca_dev);
1076         }
1077         return 0;
1078 }
1079
1080 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1081 {
1082         struct sd *sd = (struct sd *) gspca_dev;
1083
1084         *val = sd->autogain;
1085         return 0;
1086 }
1087
1088 static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val)
1089 {
1090         struct sd *sd = (struct sd *) gspca_dev;
1091
1092         sd->awb = val;
1093         if (gspca_dev->streaming)
1094                 setawb(gspca_dev);
1095         return 0;
1096 }
1097
1098 static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val)
1099 {
1100         struct sd *sd = (struct sd *) gspca_dev;
1101
1102         *val = sd->awb;
1103         return 0;
1104 }
1105
1106 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
1107 {
1108         struct sd *sd = (struct sd *) gspca_dev;
1109
1110         sd->sharpness = val;
1111         if (gspca_dev->streaming)
1112                 setsharpness(gspca_dev);
1113         return 0;
1114 }
1115
1116 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
1117 {
1118         struct sd *sd = (struct sd *) gspca_dev;
1119
1120         *val = sd->sharpness;
1121         return 0;
1122 }
1123
1124 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
1125 {
1126         struct sd *sd = (struct sd *) gspca_dev;
1127
1128         sd->hflip = val;
1129         if (gspca_dev->streaming)
1130                 sethflip(gspca_dev);
1131         return 0;
1132 }
1133
1134 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
1135 {
1136         struct sd *sd = (struct sd *) gspca_dev;
1137
1138         *val = sd->hflip;
1139         return 0;
1140 }
1141
1142 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
1143 {
1144         struct sd *sd = (struct sd *) gspca_dev;
1145
1146         sd->vflip = val;
1147         if (gspca_dev->streaming)
1148                 setvflip(gspca_dev);
1149         return 0;
1150 }
1151
1152 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
1153 {
1154         struct sd *sd = (struct sd *) gspca_dev;
1155
1156         *val = sd->vflip;
1157         return 0;
1158 }
1159
1160 /* get stream parameters (framerate) */
1161 static int sd_get_streamparm(struct gspca_dev *gspca_dev,
1162                              struct v4l2_streamparm *parm)
1163 {
1164         struct v4l2_captureparm *cp = &parm->parm.capture;
1165         struct v4l2_fract *tpf = &cp->timeperframe;
1166         struct sd *sd = (struct sd *) gspca_dev;
1167
1168         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1169                 return -EINVAL;
1170
1171         cp->capability |= V4L2_CAP_TIMEPERFRAME;
1172         tpf->numerator = 1;
1173         tpf->denominator = sd->frame_rate;
1174
1175         return 0;
1176 }
1177
1178 /* set stream parameters (framerate) */
1179 static int sd_set_streamparm(struct gspca_dev *gspca_dev,
1180                              struct v4l2_streamparm *parm)
1181 {
1182         struct v4l2_captureparm *cp = &parm->parm.capture;
1183         struct v4l2_fract *tpf = &cp->timeperframe;
1184         struct sd *sd = (struct sd *) gspca_dev;
1185
1186         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1187                 return -EINVAL;
1188
1189         /* Set requested framerate */
1190         sd->frame_rate = tpf->denominator / tpf->numerator;
1191         if (gspca_dev->streaming)
1192                 set_frame_rate(gspca_dev);
1193
1194         /* Return the actual framerate */
1195         tpf->numerator = 1;
1196         tpf->denominator = sd->frame_rate;
1197
1198         return 0;
1199 }
1200
1201 /* sub-driver description */
1202 static const struct sd_desc sd_desc = {
1203         .name     = MODULE_NAME,
1204         .ctrls    = sd_ctrls,
1205         .nctrls   = ARRAY_SIZE(sd_ctrls),
1206         .config   = sd_config,
1207         .init     = sd_init,
1208         .start    = sd_start,
1209         .stopN    = sd_stopN,
1210         .pkt_scan = sd_pkt_scan,
1211         .get_streamparm = sd_get_streamparm,
1212         .set_streamparm = sd_set_streamparm,
1213 };
1214
1215 /* -- module initialisation -- */
1216 static const __devinitdata struct usb_device_id device_table[] = {
1217         {USB_DEVICE(0x1415, 0x2000)},
1218         {}
1219 };
1220
1221 MODULE_DEVICE_TABLE(usb, device_table);
1222
1223 /* -- device connect -- */
1224 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1225 {
1226         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1227                                 THIS_MODULE);
1228 }
1229
1230 static struct usb_driver sd_driver = {
1231         .name       = MODULE_NAME,
1232         .id_table   = device_table,
1233         .probe      = sd_probe,
1234         .disconnect = gspca_disconnect,
1235 #ifdef CONFIG_PM
1236         .suspend    = gspca_suspend,
1237         .resume     = gspca_resume,
1238 #endif
1239 };
1240
1241 /* -- module insert / remove -- */
1242 static int __init sd_mod_init(void)
1243 {
1244         int ret;
1245
1246         ret = usb_register(&sd_driver);
1247         if (ret < 0)
1248                 return ret;
1249         PDEBUG(D_PROBE, "registered");
1250         return 0;
1251 }
1252
1253 static void __exit sd_mod_exit(void)
1254 {
1255         usb_deregister(&sd_driver);
1256         PDEBUG(D_PROBE, "deregistered");
1257 }
1258
1259 module_init(sd_mod_init);
1260 module_exit(sd_mod_exit);