2 * ov534-ov965x gspca driver
4 * Copyright (C) 2009-2010 Jean-Francois Moine http://moinejf.free.fr
5 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
6 * Copyright (C) 2008 Jim Paris <jim@jtan.com>
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/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define MODULE_NAME "ov534_9"
31 #define OV534_REG_ADDRESS 0xf1 /* sensor address */
32 #define OV534_REG_SUBADDR 0xf2
33 #define OV534_REG_WRITE 0xf3
34 #define OV534_REG_READ 0xf4
35 #define OV534_REG_OPERATION 0xf5
36 #define OV534_REG_STATUS 0xf6
38 #define OV534_OP_WRITE_3 0x37
39 #define OV534_OP_WRITE_2 0x33
40 #define OV534_OP_READ_2 0xf9
42 #define CTRL_TIMEOUT 500
44 MODULE_AUTHOR("Jean-Francois Moine <moinejf@free.fr>");
45 MODULE_DESCRIPTION("GSPCA/OV534_9 USB Camera Driver");
46 MODULE_LICENSE("GPL");
48 /* specific webcam descriptor */
50 struct gspca_dev gspca_dev
; /* !! must be the first item */
63 /* V4L2 controls supported by the driver */
64 static int sd_setbrightness(struct gspca_dev
*gspca_dev
, __s32 val
);
65 static int sd_getbrightness(struct gspca_dev
*gspca_dev
, __s32
*val
);
66 static int sd_setcontrast(struct gspca_dev
*gspca_dev
, __s32 val
);
67 static int sd_getcontrast(struct gspca_dev
*gspca_dev
, __s32
*val
);
68 static int sd_setautogain(struct gspca_dev
*gspca_dev
, __s32 val
);
69 static int sd_getautogain(struct gspca_dev
*gspca_dev
, __s32
*val
);
70 static int sd_setexposure(struct gspca_dev
*gspca_dev
, __s32 val
);
71 static int sd_getexposure(struct gspca_dev
*gspca_dev
, __s32
*val
);
72 static int sd_setsharpness(struct gspca_dev
*gspca_dev
, __s32 val
);
73 static int sd_getsharpness(struct gspca_dev
*gspca_dev
, __s32
*val
);
74 static int sd_setsatur(struct gspca_dev
*gspca_dev
, __s32 val
);
75 static int sd_getsatur(struct gspca_dev
*gspca_dev
, __s32
*val
);
76 static int sd_setfreq(struct gspca_dev
*gspca_dev
, __s32 val
);
77 static int sd_getfreq(struct gspca_dev
*gspca_dev
, __s32
*val
);
79 static const struct ctrl sd_ctrls
[] = {
82 .id
= V4L2_CID_BRIGHTNESS
,
83 .type
= V4L2_CTRL_TYPE_INTEGER
,
88 #define BRIGHTNESS_DEF 7
89 .default_value
= BRIGHTNESS_DEF
,
91 .set
= sd_setbrightness
,
92 .get
= sd_getbrightness
,
96 .id
= V4L2_CID_CONTRAST
,
97 .type
= V4L2_CTRL_TYPE_INTEGER
,
102 #define CONTRAST_DEF 3
103 .default_value
= CONTRAST_DEF
,
105 .set
= sd_setcontrast
,
106 .get
= sd_getcontrast
,
110 .id
= V4L2_CID_AUTOGAIN
,
111 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
116 #define AUTOGAIN_DEF 1
117 .default_value
= AUTOGAIN_DEF
,
119 .set
= sd_setautogain
,
120 .get
= sd_getautogain
,
125 .id
= V4L2_CID_EXPOSURE
,
126 .type
= V4L2_CTRL_TYPE_INTEGER
,
132 .default_value
= EXPO_DEF
,
134 .set
= sd_setexposure
,
135 .get
= sd_getexposure
,
139 .id
= V4L2_CID_SHARPNESS
,
140 .type
= V4L2_CTRL_TYPE_INTEGER
,
142 .minimum
= -1, /* -1 = auto */
145 #define SHARPNESS_DEF -1
146 .default_value
= SHARPNESS_DEF
,
148 .set
= sd_setsharpness
,
149 .get
= sd_getsharpness
,
153 .id
= V4L2_CID_SATURATION
,
154 .type
= V4L2_CTRL_TYPE_INTEGER
,
155 .name
= "Saturation",
160 .default_value
= SATUR_DEF
,
167 .id
= V4L2_CID_POWER_LINE_FREQUENCY
,
168 .type
= V4L2_CTRL_TYPE_MENU
,
169 .name
= "Light frequency filter",
171 .maximum
= 2, /* 0: 0, 1: 50Hz, 2:60Hz */
174 .default_value
= FREQ_DEF
,
181 static const struct v4l2_pix_format ov965x_mode
[] = {
183 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
185 .sizeimage
= 320 * 240 * 3 / 8 + 590,
186 .colorspace
= V4L2_COLORSPACE_JPEG
},
188 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
190 .sizeimage
= 640 * 480 * 3 / 8 + 590,
191 .colorspace
= V4L2_COLORSPACE_JPEG
},
193 {800, 600, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
195 .sizeimage
= 800 * 600 * 3 / 8 + 590,
196 .colorspace
= V4L2_COLORSPACE_JPEG
},
198 {1024, 768, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
199 .bytesperline
= 1024,
200 .sizeimage
= 1024 * 768 * 3 / 8 + 590,
201 .colorspace
= V4L2_COLORSPACE_JPEG
},
203 {1280, 1024, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
204 .bytesperline
= 1280,
205 .sizeimage
= 1280 * 1024 * 3 / 8 + 590,
206 .colorspace
= V4L2_COLORSPACE_JPEG
},
209 static const u8 bridge_init
[][2] = {
243 static const u8 sensor_init
[][2] = {
244 {0x12, 0x80}, /* com7 - SSCB reset */
245 {0x00, 0x00}, /* gain */
246 {0x01, 0x80}, /* blue */
247 {0x02, 0x80}, /* red */
248 {0x03, 0x1b}, /* vref */
249 {0x04, 0x03}, /* com1 - exposure low bits */
250 {0x0b, 0x57}, /* ver */
251 {0x0e, 0x61}, /* com5 */
252 {0x0f, 0x42}, /* com6 */
253 {0x11, 0x00}, /* clkrc */
254 {0x12, 0x02}, /* com7 - 15fps VGA YUYV */
255 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
256 {0x14, 0x28}, /* com9 */
257 {0x16, 0x24}, /* reg16 */
258 {0x17, 0x1d}, /* hstart*/
259 {0x18, 0xbd}, /* hstop */
260 {0x19, 0x01}, /* vstrt */
261 {0x1a, 0x81}, /* vstop*/
262 {0x1e, 0x04}, /* mvfp */
263 {0x24, 0x3c}, /* aew */
264 {0x25, 0x36}, /* aeb */
265 {0x26, 0x71}, /* vpt */
266 {0x27, 0x08}, /* bbias */
267 {0x28, 0x08}, /* gbbias */
268 {0x29, 0x15}, /* gr com */
269 {0x2a, 0x00}, /* exhch */
270 {0x2b, 0x00}, /* exhcl */
271 {0x2c, 0x08}, /* rbias */
272 {0x32, 0xff}, /* href */
273 {0x33, 0x00}, /* chlf */
274 {0x34, 0x3f}, /* aref1 */
275 {0x35, 0x00}, /* aref2 */
276 {0x36, 0xf8}, /* aref3 */
277 {0x38, 0x72}, /* adc2 */
278 {0x39, 0x57}, /* aref4 */
279 {0x3a, 0x80}, /* tslb - yuyv */
280 {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
281 {0x3d, 0x99}, /* com13 */
282 {0x3f, 0xc1}, /* edge */
283 {0x40, 0xc0}, /* com15 */
284 {0x41, 0x40}, /* com16 */
285 {0x42, 0xc0}, /* com17 */
286 {0x43, 0x0a}, /* rsvd */
297 {0x4f, 0x98}, /* matrix */
303 {0x58, 0x1a}, /* matrix coef sign */
304 {0x59, 0x85}, /* AWB control */
310 {0x5f, 0xf0}, /* AWB blue limit */
311 {0x60, 0xf0}, /* AWB red limit */
312 {0x61, 0xf0}, /* AWB green limit */
313 {0x62, 0x00}, /* lcc1 */
314 {0x63, 0x00}, /* lcc2 */
315 {0x64, 0x02}, /* lcc3 */
316 {0x65, 0x16}, /* lcc4 */
317 {0x66, 0x01}, /* lcc5 */
318 {0x69, 0x02}, /* hv */
319 {0x6b, 0x5a}, /* dbvl */
324 {0x70, 0x21}, /* dnsth */
326 {0x72, 0x00}, /* poidx */
327 {0x73, 0x01}, /* pckdv */
328 {0x74, 0x3a}, /* xindx */
329 {0x75, 0x35}, /* yindx */
332 {0x7a, 0x12}, /* gamma curve */
349 {0x8c, 0x89}, /* com19 */
350 {0x14, 0x28}, /* com9 */
353 {0x9d, 0x03}, /* lcc6 */
354 {0x9e, 0x04}, /* lcc7 */
357 {0xa1, 0x40}, /* aechm */
358 {0xa4, 0x50}, /* com21 */
359 {0xa5, 0x68}, /* com26 */
360 {0xa6, 0x4a}, /* AWB green */
361 {0xa8, 0xc1}, /* refa8 */
362 {0xa9, 0xef}, /* refa9 */
365 {0xac, 0x80}, /* black level control */
371 {0xb4, 0x20}, /* ctrlb4 */
375 {0xbc, 0x7f}, /* ADC channel offsets */
384 {0xc7, 0x80}, /* com24 */
390 {0x4f, 0x98}, /* matrix */
397 {0xff, 0x41}, /* read 41, write ff 00 */
398 {0x41, 0x40}, /* com16 */
400 {0xc5, 0x03}, /* 60 Hz banding filter */
401 {0x6a, 0x02}, /* 50 Hz banding filter */
403 {0x12, 0x62}, /* com7 - 30fps VGA YUV */
404 {0x36, 0xfa}, /* aref3 */
405 {0x69, 0x0a}, /* hv */
406 {0x8c, 0x89}, /* com22 */
407 {0x14, 0x28}, /* com9 */
409 {0x41, 0x40}, /* com16 */
416 {0x03, 0x12}, /* vref */
417 {0x17, 0x16}, /* hstart */
418 {0x18, 0x02}, /* hstop */
419 {0x19, 0x01}, /* vstrt */
420 {0x1a, 0x3d}, /* vstop */
421 {0x32, 0xff}, /* href */
425 static const u8 bridge_init_2
[][2] = {
453 static const u8 sensor_init_2
[][2] = {
455 {0x1e, 0x04}, /* mvfp */
456 {0x13, 0xe0}, /* com8 */
457 {0x00, 0x00}, /* gain */
458 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
459 {0x11, 0x03}, /* clkrc */
460 {0x6b, 0x5a}, /* dblv */
466 {0xff, 0x42}, /* read 42, write ff 00 */
467 {0x42, 0xc0}, /* com17 */
469 {0xff, 0x42}, /* read 42, write ff 00 */
470 {0x42, 0xc1}, /* com17 */
473 {0xff, 0x42}, /* read 42, write ff 00 */
474 {0x42, 0xc1}, /* com17 */
476 {0x4f, 0x98}, /* matrix */
483 {0xff, 0x41}, /* read 41, write ff 00 */
484 {0x41, 0x40}, /* com16 */
490 {0x10, 0x25}, /* aech - exposure high bits */
491 {0xff, 0x13}, /* read 13, write ff 00 */
492 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
495 static const u8 sensor_start_1_vga
[][2] = { /* same for qvga */
496 {0x12, 0x62}, /* com7 - 30fps VGA YUV */
497 {0x36, 0xfa}, /* aref3 */
498 {0x69, 0x0a}, /* hv */
499 {0x8c, 0x89}, /* com22 */
500 {0x14, 0x28}, /* com9 */
501 {0x3e, 0x0c}, /* com14 */
502 {0x41, 0x40}, /* com16 */
508 {0xc7, 0x80}, /* com24 */
509 {0x03, 0x12}, /* vref */
510 {0x17, 0x16}, /* hstart */
511 {0x18, 0x02}, /* hstop */
512 {0x19, 0x01}, /* vstrt */
513 {0x1a, 0x3d}, /* vstop */
514 {0x32, 0xff}, /* href */
518 static const u8 sensor_start_1_svga
[][2] = {
519 {0x12, 0x02}, /* com7 - YUYV - VGA 15 full resolution */
520 {0x36, 0xf8}, /* aref3 */
521 {0x69, 0x02}, /* hv */
522 {0x8c, 0x0d}, /* com22 */
523 {0x3e, 0x0c}, /* com14 */
524 {0x41, 0x40}, /* com16 */
530 {0xc7, 0x80}, /* com24 */
531 {0x03, 0x1b}, /* vref */
532 {0x17, 0x1d}, /* hstart */
533 {0x18, 0xbd}, /* hstop */
534 {0x19, 0x01}, /* vstrt */
535 {0x1a, 0x81}, /* vstop */
536 {0x32, 0xff}, /* href */
540 static const u8 sensor_start_1_xga
[][2] = {
541 {0x12, 0x02}, /* com7 */
542 {0x36, 0xf8}, /* aref3 */
543 {0x69, 0x02}, /* hv */
544 {0x8c, 0x89}, /* com22 */
545 {0x14, 0x28}, /* com9 */
546 {0x3e, 0x0c}, /* com14 */
547 {0x41, 0x40}, /* com16 */
553 {0xc7, 0x80}, /* com24 */
554 {0x03, 0x1b}, /* vref */
555 {0x17, 0x1d}, /* hstart */
556 {0x18, 0xbd}, /* hstop */
557 {0x19, 0x01}, /* vstrt */
558 {0x1a, 0x81}, /* vstop */
559 {0x32, 0xff}, /* href */
563 static const u8 sensor_start_1_sxga
[][2] = {
564 {0x12, 0x02}, /* com7 */
565 {0x36, 0xf8}, /* aref3 */
566 {0x69, 0x02}, /* hv */
567 {0x8c, 0x89}, /* com22 */
568 {0x14, 0x28}, /* com9 */
569 {0x3e, 0x0c}, /* com14 */
570 {0x41, 0x40}, /* com16 */
576 {0xc7, 0x80}, /* com24 */
577 {0x03, 0x1b}, /* vref */
578 {0x17, 0x1d}, /* hstart */
579 {0x18, 0x02}, /* hstop */
580 {0x19, 0x01}, /* vstrt */
581 {0x1a, 0x81}, /* vstop */
582 {0x32, 0xff}, /* href */
586 static const u8 bridge_start_qvga
[][2] = {
614 static const u8 bridge_start_vga
[][2] = {
641 static const u8 bridge_start_svga
[][2] = {
668 static const u8 bridge_start_xga
[][2] = {
695 static const u8 bridge_start_sxga
[][2] = {
712 static const u8 sensor_start_2_qvga
[][2] = {
713 {0x3b, 0xe4}, /* com11 - night mode 1/4 frame rate */
714 {0x1e, 0x04}, /* mvfp */
715 {0x13, 0xe0}, /* com8 */
717 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
718 {0x11, 0x01}, /* clkrc */
719 {0x6b, 0x5a}, /* dblv */
720 {0x6a, 0x02}, /* 50 Hz banding filter */
721 {0xc5, 0x03}, /* 60 Hz banding filter */
722 {0xa2, 0x96}, /* bd50 */
723 {0xa3, 0x7d}, /* bd60 */
725 {0xff, 0x13}, /* read 13, write ff 00 */
727 {0x3a, 0x80}, /* tslb - yuyv */
730 static const u8 sensor_start_2_vga
[][2] = {
731 {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
732 {0x1e, 0x04}, /* mvfp */
733 {0x13, 0xe0}, /* com8 */
735 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
736 {0x11, 0x03}, /* clkrc */
737 {0x6b, 0x5a}, /* dblv */
738 {0x6a, 0x05}, /* 50 Hz banding filter */
739 {0xc5, 0x07}, /* 60 Hz banding filter */
740 {0xa2, 0x4b}, /* bd50 */
741 {0xa3, 0x3e}, /* bd60 */
743 {0x2d, 0x00}, /* advfl */
746 static const u8 sensor_start_2_svga
[][2] = { /* same for xga */
747 {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
748 {0x1e, 0x04}, /* mvfp */
749 {0x13, 0xe0}, /* com8 */
751 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
752 {0x11, 0x01}, /* clkrc */
753 {0x6b, 0x5a}, /* dblv */
754 {0x6a, 0x0c}, /* 50 Hz banding filter */
755 {0xc5, 0x0f}, /* 60 Hz banding filter */
756 {0xa2, 0x4e}, /* bd50 */
757 {0xa3, 0x41}, /* bd60 */
760 static const u8 sensor_start_2_sxga
[][2] = {
761 {0x13, 0xe0}, /* com8 */
763 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
764 {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
765 {0x1e, 0x04}, /* mvfp */
766 {0x11, 0x01}, /* clkrc */
767 {0x6b, 0x5a}, /* dblv */
768 {0x6a, 0x0c}, /* 50 Hz banding filter */
769 {0xc5, 0x0f}, /* 60 Hz banding filter */
770 {0xa2, 0x4e}, /* bd50 */
771 {0xa3, 0x41}, /* bd60 */
774 static void reg_w_i(struct gspca_dev
*gspca_dev
, u16 reg
, u8 val
)
776 struct usb_device
*udev
= gspca_dev
->dev
;
779 if (gspca_dev
->usb_err
< 0)
781 gspca_dev
->usb_buf
[0] = val
;
782 ret
= usb_control_msg(udev
,
783 usb_sndctrlpipe(udev
, 0),
785 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
786 0x00, reg
, gspca_dev
->usb_buf
, 1, CTRL_TIMEOUT
);
788 PDEBUG(D_ERR
, "reg_w failed %d", ret
);
789 gspca_dev
->usb_err
= ret
;
793 static void reg_w(struct gspca_dev
*gspca_dev
, u16 reg
, u8 val
)
795 PDEBUG(D_USBO
, "reg_w [%04x] = %02x", reg
, val
);
796 reg_w_i(gspca_dev
, reg
, val
);
799 static u8
reg_r(struct gspca_dev
*gspca_dev
, u16 reg
)
801 struct usb_device
*udev
= gspca_dev
->dev
;
804 if (gspca_dev
->usb_err
< 0)
806 ret
= usb_control_msg(udev
,
807 usb_rcvctrlpipe(udev
, 0),
809 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
810 0x00, reg
, gspca_dev
->usb_buf
, 1, CTRL_TIMEOUT
);
811 PDEBUG(D_USBI
, "reg_r [%04x] -> %02x", reg
, gspca_dev
->usb_buf
[0]);
813 PDEBUG(D_ERR
, "reg_r err %d", ret
);
814 gspca_dev
->usb_err
= ret
;
816 return gspca_dev
->usb_buf
[0];
819 static int sccb_check_status(struct gspca_dev
*gspca_dev
)
824 for (i
= 0; i
< 5; i
++) {
825 data
= reg_r(gspca_dev
, OV534_REG_STATUS
);
835 PDEBUG(D_USBI
|D_USBO
,
836 "sccb status 0x%02x, attempt %d/5",
843 static void sccb_write(struct gspca_dev
*gspca_dev
, u8 reg
, u8 val
)
845 PDEBUG(D_USBO
, "sccb_write [%02x] = %02x", reg
, val
);
846 reg_w_i(gspca_dev
, OV534_REG_SUBADDR
, reg
);
847 reg_w_i(gspca_dev
, OV534_REG_WRITE
, val
);
848 reg_w_i(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_WRITE_3
);
850 if (!sccb_check_status(gspca_dev
))
851 PDEBUG(D_ERR
, "sccb_write failed");
854 static u8
sccb_read(struct gspca_dev
*gspca_dev
, u16 reg
)
856 reg_w(gspca_dev
, OV534_REG_SUBADDR
, reg
);
857 reg_w(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_WRITE_2
);
858 if (!sccb_check_status(gspca_dev
))
859 PDEBUG(D_ERR
, "sccb_read failed 1");
861 reg_w(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_READ_2
);
862 if (!sccb_check_status(gspca_dev
))
863 PDEBUG(D_ERR
, "sccb_read failed 2");
865 return reg_r(gspca_dev
, OV534_REG_READ
);
868 /* output a bridge sequence (reg - val) */
869 static void reg_w_array(struct gspca_dev
*gspca_dev
,
870 const u8 (*data
)[2], int len
)
873 reg_w(gspca_dev
, (*data
)[0], (*data
)[1]);
878 /* output a sensor sequence (reg - val) */
879 static void sccb_w_array(struct gspca_dev
*gspca_dev
,
880 const u8 (*data
)[2], int len
)
883 if ((*data
)[0] != 0xff) {
884 sccb_write(gspca_dev
, (*data
)[0], (*data
)[1]);
886 sccb_read(gspca_dev
, (*data
)[1]);
887 sccb_write(gspca_dev
, 0xff, 0x00);
893 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
894 * (direction and output)? */
895 static void set_led(struct gspca_dev
*gspca_dev
, int status
)
899 PDEBUG(D_CONF
, "led status: %d", status
);
901 data
= reg_r(gspca_dev
, 0x21);
903 reg_w(gspca_dev
, 0x21, data
);
905 data
= reg_r(gspca_dev
, 0x23);
911 reg_w(gspca_dev
, 0x23, data
);
914 data
= reg_r(gspca_dev
, 0x21);
916 reg_w(gspca_dev
, 0x21, data
);
920 static void setbrightness(struct gspca_dev
*gspca_dev
)
922 struct sd
*sd
= (struct sd
*) gspca_dev
;
925 val
= sd
->brightness
;
927 val
= 15 - val
; /* f .. 8 */
929 val
= val
- 8; /* 0 .. 7 */
930 sccb_write(gspca_dev
, 0x55, /* brtn - brightness adjustment */
934 static void setcontrast(struct gspca_dev
*gspca_dev
)
936 struct sd
*sd
= (struct sd
*) gspca_dev
;
938 sccb_write(gspca_dev
, 0x56, /* cnst1 - contrast 1 ctrl coeff */
942 static void setautogain(struct gspca_dev
*gspca_dev
)
944 struct sd
*sd
= (struct sd
*) gspca_dev
;
947 /*fixme: should adjust agc/awb/aec by different controls */
949 val
= sccb_read(gspca_dev
, 0x13); /* com8 */
950 sccb_write(gspca_dev
, 0xff, 0x00);
952 val
|= 0x05; /* agc & aec */
955 sccb_write(gspca_dev
, 0x13, val
);
958 static void setexposure(struct gspca_dev
*gspca_dev
)
960 struct sd
*sd
= (struct sd
*) gspca_dev
;
962 static const u8 expo
[4] = {0x00, 0x25, 0x38, 0x5e};
964 sccb_write(gspca_dev
, 0x10, /* aec[9:2] */
967 val
= sccb_read(gspca_dev
, 0x13); /* com8 */
968 sccb_write(gspca_dev
, 0xff, 0x00);
969 sccb_write(gspca_dev
, 0x13, val
);
971 val
= sccb_read(gspca_dev
, 0xa1); /* aech */
972 sccb_write(gspca_dev
, 0xff, 0x00);
973 sccb_write(gspca_dev
, 0xa1, val
& 0xe0); /* aec[15:10] = 0 */
976 static void setsharpness(struct gspca_dev
*gspca_dev
)
978 struct sd
*sd
= (struct sd
*) gspca_dev
;
982 if (val
< 0) { /* auto */
983 val
= sccb_read(gspca_dev
, 0x42); /* com17 */
984 sccb_write(gspca_dev
, 0xff, 0x00);
985 sccb_write(gspca_dev
, 0x42, val
| 0x40);
986 /* Edge enhancement strength auto adjust */
990 val
= 1 << (val
- 1);
991 sccb_write(gspca_dev
, 0x3f, /* edge - edge enhance. factor */
993 val
= sccb_read(gspca_dev
, 0x42); /* com17 */
994 sccb_write(gspca_dev
, 0xff, 0x00);
995 sccb_write(gspca_dev
, 0x42, val
& 0xbf);
998 static void setsatur(struct gspca_dev
*gspca_dev
)
1000 struct sd
*sd
= (struct sd
*) gspca_dev
;
1001 u8 val1
, val2
, val3
;
1002 static const u8 matrix
[5][2] = {
1010 val1
= matrix
[sd
->satur
][0];
1011 val2
= matrix
[sd
->satur
][1];
1013 sccb_write(gspca_dev
, 0x4f, val3
); /* matrix coeff */
1014 sccb_write(gspca_dev
, 0x50, val3
);
1015 sccb_write(gspca_dev
, 0x51, 0x00);
1016 sccb_write(gspca_dev
, 0x52, val1
);
1017 sccb_write(gspca_dev
, 0x53, val2
);
1018 sccb_write(gspca_dev
, 0x54, val3
);
1019 sccb_write(gspca_dev
, 0x58, 0x1a); /* mtxs - coeff signs */
1021 val1
= sccb_read(gspca_dev
, 0x41); /* com16 */
1022 sccb_write(gspca_dev
, 0xff, 0x00);
1023 sccb_write(gspca_dev
, 0x41, val1
);
1026 static void setfreq(struct gspca_dev
*gspca_dev
)
1028 struct sd
*sd
= (struct sd
*) gspca_dev
;
1031 val
= sccb_read(gspca_dev
, 0x13); /* com8 */
1032 sccb_write(gspca_dev
, 0xff, 0x00);
1033 if (sd
->freq
== 0) {
1034 sccb_write(gspca_dev
, 0x13, val
& 0xdf);
1037 sccb_write(gspca_dev
, 0x13, val
| 0x20);
1039 val
= sccb_read(gspca_dev
, 0x42); /* com17 */
1040 sccb_write(gspca_dev
, 0xff, 0x00);
1045 sccb_write(gspca_dev
, 0x42, val
);
1048 /* this function is called at probe time */
1049 static int sd_config(struct gspca_dev
*gspca_dev
,
1050 const struct usb_device_id
*id
)
1052 struct sd
*sd
= (struct sd
*) gspca_dev
;
1055 cam
= &gspca_dev
->cam
;
1057 cam
->cam_mode
= ov965x_mode
;
1058 cam
->nmodes
= ARRAY_SIZE(ov965x_mode
);
1060 sd
->brightness
= BRIGHTNESS_DEF
;
1061 sd
->contrast
= CONTRAST_DEF
;
1062 #if AUTOGAIN_DEF != 0
1063 sd
->autogain
= AUTOGAIN_DEF
;
1064 gspca_dev
->ctrl_inac
|= (1 << EXPO_IDX
);
1067 sd
->exposure
= EXPO_DEF
;
1069 #if SHARPNESS_DEF != 0
1070 sd
->sharpness
= SHARPNESS_DEF
;
1072 sd
->satur
= SATUR_DEF
;
1073 sd
->freq
= FREQ_DEF
;
1078 /* this function is called at probe and resume time */
1079 static int sd_init(struct gspca_dev
*gspca_dev
)
1084 reg_w(gspca_dev
, 0xe7, 0x3a);
1085 reg_w(gspca_dev
, 0xe0, 0x08);
1088 /* initialize the sensor address */
1089 reg_w(gspca_dev
, OV534_REG_ADDRESS
, 0x60);
1092 sccb_write(gspca_dev
, 0x12, 0x80);
1095 /* probe the sensor */
1096 sccb_read(gspca_dev
, 0x0a);
1097 sensor_id
= sccb_read(gspca_dev
, 0x0a) << 8;
1098 sccb_read(gspca_dev
, 0x0b);
1099 sensor_id
|= sccb_read(gspca_dev
, 0x0b);
1100 PDEBUG(D_PROBE
, "Sensor ID: %04x", sensor_id
);
1103 reg_w_array(gspca_dev
, bridge_init
,
1104 ARRAY_SIZE(bridge_init
));
1105 sccb_w_array(gspca_dev
, sensor_init
,
1106 ARRAY_SIZE(sensor_init
));
1107 reg_w_array(gspca_dev
, bridge_init_2
,
1108 ARRAY_SIZE(bridge_init_2
));
1109 sccb_w_array(gspca_dev
, sensor_init_2
,
1110 ARRAY_SIZE(sensor_init_2
));
1111 reg_w(gspca_dev
, 0xe0, 0x00);
1112 reg_w(gspca_dev
, 0xe0, 0x01);
1113 set_led(gspca_dev
, 0);
1114 reg_w(gspca_dev
, 0xe0, 0x00);
1116 return gspca_dev
->usb_err
;
1119 static int sd_start(struct gspca_dev
*gspca_dev
)
1121 switch (gspca_dev
->curr_mode
) {
1122 case QVGA_MODE
: /* 320x240 */
1123 sccb_w_array(gspca_dev
, sensor_start_1_vga
,
1124 ARRAY_SIZE(sensor_start_1_vga
));
1125 reg_w_array(gspca_dev
, bridge_start_qvga
,
1126 ARRAY_SIZE(bridge_start_qvga
));
1127 sccb_w_array(gspca_dev
, sensor_start_2_qvga
,
1128 ARRAY_SIZE(sensor_start_2_qvga
));
1130 case VGA_MODE
: /* 640x480 */
1131 sccb_w_array(gspca_dev
, sensor_start_1_vga
,
1132 ARRAY_SIZE(sensor_start_1_vga
));
1133 reg_w_array(gspca_dev
, bridge_start_vga
,
1134 ARRAY_SIZE(bridge_start_vga
));
1135 sccb_w_array(gspca_dev
, sensor_start_2_vga
,
1136 ARRAY_SIZE(sensor_start_2_vga
));
1138 case SVGA_MODE
: /* 800x600 */
1139 sccb_w_array(gspca_dev
, sensor_start_1_svga
,
1140 ARRAY_SIZE(sensor_start_1_svga
));
1141 reg_w_array(gspca_dev
, bridge_start_svga
,
1142 ARRAY_SIZE(bridge_start_svga
));
1143 sccb_w_array(gspca_dev
, sensor_start_2_svga
,
1144 ARRAY_SIZE(sensor_start_2_svga
));
1146 case XGA_MODE
: /* 1024x768 */
1147 sccb_w_array(gspca_dev
, sensor_start_1_xga
,
1148 ARRAY_SIZE(sensor_start_1_xga
));
1149 reg_w_array(gspca_dev
, bridge_start_xga
,
1150 ARRAY_SIZE(bridge_start_xga
));
1151 sccb_w_array(gspca_dev
, sensor_start_2_svga
,
1152 ARRAY_SIZE(sensor_start_2_svga
));
1155 /* case SXGA_MODE: * 1280x1024 */
1156 sccb_w_array(gspca_dev
, sensor_start_1_sxga
,
1157 ARRAY_SIZE(sensor_start_1_sxga
));
1158 reg_w_array(gspca_dev
, bridge_start_sxga
,
1159 ARRAY_SIZE(bridge_start_sxga
));
1160 sccb_w_array(gspca_dev
, sensor_start_2_sxga
,
1161 ARRAY_SIZE(sensor_start_2_sxga
));
1165 setautogain(gspca_dev
);
1166 setbrightness(gspca_dev
);
1167 setcontrast(gspca_dev
);
1168 setexposure(gspca_dev
);
1169 setsharpness(gspca_dev
);
1170 setsatur(gspca_dev
);
1172 reg_w(gspca_dev
, 0xe0, 0x00);
1173 reg_w(gspca_dev
, 0xe0, 0x00);
1174 set_led(gspca_dev
, 1);
1175 return gspca_dev
->usb_err
;
1178 static void sd_stopN(struct gspca_dev
*gspca_dev
)
1180 reg_w(gspca_dev
, 0xe0, 0x01);
1181 set_led(gspca_dev
, 0);
1182 reg_w(gspca_dev
, 0xe0, 0x00);
1185 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1186 #define UVC_STREAM_EOH (1 << 7)
1187 #define UVC_STREAM_ERR (1 << 6)
1188 #define UVC_STREAM_STI (1 << 5)
1189 #define UVC_STREAM_RES (1 << 4)
1190 #define UVC_STREAM_SCR (1 << 3)
1191 #define UVC_STREAM_PTS (1 << 2)
1192 #define UVC_STREAM_EOF (1 << 1)
1193 #define UVC_STREAM_FID (1 << 0)
1195 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
1198 struct sd
*sd
= (struct sd
*) gspca_dev
;
1201 int remaining_len
= len
;
1204 len
= min(remaining_len
, 2040);
1206 /* Payloads are prefixed with a UVC-style header. We
1207 consider a frame to start when the FID toggles, or the PTS
1208 changes. A frame ends when EOF is set, and we've received
1209 the correct number of bytes. */
1211 /* Verify UVC header. Header length is always 12 */
1212 if (data
[0] != 12 || len
< 12) {
1213 PDEBUG(D_PACK
, "bad header");
1218 if (data
[1] & UVC_STREAM_ERR
) {
1219 PDEBUG(D_PACK
, "payload error");
1223 /* Extract PTS and FID */
1224 if (!(data
[1] & UVC_STREAM_PTS
)) {
1225 PDEBUG(D_PACK
, "PTS not present");
1228 this_pts
= (data
[5] << 24) | (data
[4] << 16)
1229 | (data
[3] << 8) | data
[2];
1230 this_fid
= data
[1] & UVC_STREAM_FID
;
1232 /* If PTS or FID has changed, start a new frame. */
1233 if (this_pts
!= sd
->last_pts
|| this_fid
!= sd
->last_fid
) {
1234 if (gspca_dev
->last_packet_type
== INTER_PACKET
)
1235 gspca_frame_add(gspca_dev
, LAST_PACKET
,
1237 sd
->last_pts
= this_pts
;
1238 sd
->last_fid
= this_fid
;
1239 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
1240 data
+ 12, len
- 12);
1241 /* If this packet is marked as EOF, end the frame */
1242 } else if (data
[1] & UVC_STREAM_EOF
) {
1244 gspca_frame_add(gspca_dev
, LAST_PACKET
,
1245 data
+ 12, len
- 12);
1248 /* Add the data from this payload */
1249 gspca_frame_add(gspca_dev
, INTER_PACKET
,
1250 data
+ 12, len
- 12);
1253 /* Done this payload */
1257 /* Discard data until a new frame starts. */
1258 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
1261 remaining_len
-= len
;
1263 } while (remaining_len
> 0);
1267 static int sd_setbrightness(struct gspca_dev
*gspca_dev
, __s32 val
)
1269 struct sd
*sd
= (struct sd
*) gspca_dev
;
1271 sd
->brightness
= val
;
1272 if (gspca_dev
->streaming
)
1273 setbrightness(gspca_dev
);
1274 return gspca_dev
->usb_err
;
1277 static int sd_getbrightness(struct gspca_dev
*gspca_dev
, __s32
*val
)
1279 struct sd
*sd
= (struct sd
*) gspca_dev
;
1281 *val
= sd
->brightness
;
1285 static int sd_setcontrast(struct gspca_dev
*gspca_dev
, __s32 val
)
1287 struct sd
*sd
= (struct sd
*) gspca_dev
;
1290 if (gspca_dev
->streaming
)
1291 setcontrast(gspca_dev
);
1292 return gspca_dev
->usb_err
;
1295 static int sd_getcontrast(struct gspca_dev
*gspca_dev
, __s32
*val
)
1297 struct sd
*sd
= (struct sd
*) gspca_dev
;
1299 *val
= sd
->contrast
;
1303 static int sd_setautogain(struct gspca_dev
*gspca_dev
, __s32 val
)
1305 struct sd
*sd
= (struct sd
*) gspca_dev
;
1309 if (gspca_dev
->streaming
) {
1311 gspca_dev
->ctrl_inac
|= (1 << EXPO_IDX
);
1313 gspca_dev
->ctrl_inac
&= ~(1 << EXPO_IDX
);
1314 setautogain(gspca_dev
);
1316 return gspca_dev
->usb_err
;
1319 static int sd_getautogain(struct gspca_dev
*gspca_dev
, __s32
*val
)
1321 struct sd
*sd
= (struct sd
*) gspca_dev
;
1323 *val
= sd
->autogain
;
1327 static int sd_setexposure(struct gspca_dev
*gspca_dev
, __s32 val
)
1329 struct sd
*sd
= (struct sd
*) gspca_dev
;
1332 if (gspca_dev
->streaming
)
1333 setexposure(gspca_dev
);
1334 return gspca_dev
->usb_err
;
1337 static int sd_getexposure(struct gspca_dev
*gspca_dev
, __s32
*val
)
1339 struct sd
*sd
= (struct sd
*) gspca_dev
;
1341 *val
= sd
->exposure
;
1345 static int sd_setsharpness(struct gspca_dev
*gspca_dev
, __s32 val
)
1347 struct sd
*sd
= (struct sd
*) gspca_dev
;
1349 sd
->sharpness
= val
;
1350 if (gspca_dev
->streaming
)
1351 setsharpness(gspca_dev
);
1352 return gspca_dev
->usb_err
;
1355 static int sd_getsharpness(struct gspca_dev
*gspca_dev
, __s32
*val
)
1357 struct sd
*sd
= (struct sd
*) gspca_dev
;
1359 *val
= sd
->sharpness
;
1363 static int sd_setsatur(struct gspca_dev
*gspca_dev
, __s32 val
)
1365 struct sd
*sd
= (struct sd
*) gspca_dev
;
1368 if (gspca_dev
->streaming
)
1369 setsatur(gspca_dev
);
1370 return gspca_dev
->usb_err
;
1373 static int sd_getsatur(struct gspca_dev
*gspca_dev
, __s32
*val
)
1375 struct sd
*sd
= (struct sd
*) gspca_dev
;
1380 static int sd_setfreq(struct gspca_dev
*gspca_dev
, __s32 val
)
1382 struct sd
*sd
= (struct sd
*) gspca_dev
;
1385 if (gspca_dev
->streaming
)
1387 return gspca_dev
->usb_err
;
1390 static int sd_getfreq(struct gspca_dev
*gspca_dev
, __s32
*val
)
1392 struct sd
*sd
= (struct sd
*) gspca_dev
;
1398 static int sd_querymenu(struct gspca_dev
*gspca_dev
,
1399 struct v4l2_querymenu
*menu
)
1402 case V4L2_CID_POWER_LINE_FREQUENCY
:
1403 switch (menu
->index
) {
1404 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1405 strcpy((char *) menu
->name
, "NoFliker");
1407 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1408 strcpy((char *) menu
->name
, "50 Hz");
1410 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
1411 strcpy((char *) menu
->name
, "60 Hz");
1419 /* sub-driver description */
1420 static const struct sd_desc sd_desc
= {
1421 .name
= MODULE_NAME
,
1423 .nctrls
= ARRAY_SIZE(sd_ctrls
),
1424 .config
= sd_config
,
1428 .pkt_scan
= sd_pkt_scan
,
1429 .querymenu
= sd_querymenu
,
1432 /* -- module initialisation -- */
1433 static const __devinitdata
struct usb_device_id device_table
[] = {
1434 {USB_DEVICE(0x06f8, 0x3003)},
1438 MODULE_DEVICE_TABLE(usb
, device_table
);
1440 /* -- device connect -- */
1441 static int sd_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1443 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
1447 static struct usb_driver sd_driver
= {
1448 .name
= MODULE_NAME
,
1449 .id_table
= device_table
,
1451 .disconnect
= gspca_disconnect
,
1453 .suspend
= gspca_suspend
,
1454 .resume
= gspca_resume
,
1458 /* -- module insert / remove -- */
1459 static int __init
sd_mod_init(void)
1463 ret
= usb_register(&sd_driver
);
1466 PDEBUG(D_PROBE
, "registered");
1470 static void __exit
sd_mod_exit(void)
1472 usb_deregister(&sd_driver
);
1473 PDEBUG(D_PROBE
, "deregistered");
1476 module_init(sd_mod_init
);
1477 module_exit(sd_mod_exit
);