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 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 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 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 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 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 */
948 val = sccb_read(gspca_dev, 0x13); /* com8 */
949 sccb_write(gspca_dev, 0xff, 0x00);
951 val |= 0x05; /* agc & aec */
954 sccb_write(gspca_dev, 0x13, val);
957 static void setexposure(struct gspca_dev *gspca_dev)
959 struct sd *sd = (struct sd *) gspca_dev;
961 static const u8 expo[4] = {0x00, 0x25, 0x38, 0x5e};
963 sccb_write(gspca_dev, 0x10, /* aec[9:2] */
966 val = sccb_read(gspca_dev, 0x13); /* com8 */
967 sccb_write(gspca_dev, 0xff, 0x00);
968 sccb_write(gspca_dev, 0x13, val);
970 val = sccb_read(gspca_dev, 0xa1); /* aech */
971 sccb_write(gspca_dev, 0xff, 0x00);
972 sccb_write(gspca_dev, 0xa1, val & 0xe0); /* aec[15:10] = 0 */
975 static void setsharpness(struct gspca_dev *gspca_dev)
977 struct sd *sd = (struct sd *) gspca_dev;
981 if (val < 0) { /* auto */
982 val = sccb_read(gspca_dev, 0x42); /* com17 */
983 sccb_write(gspca_dev, 0xff, 0x00);
984 sccb_write(gspca_dev, 0x42, val | 0x40);
985 /* Edge enhancement strength auto adjust */
989 val = 1 << (val - 1);
990 sccb_write(gspca_dev, 0x3f, /* edge - edge enhance. factor */
992 val = sccb_read(gspca_dev, 0x42); /* com17 */
993 sccb_write(gspca_dev, 0xff, 0x00);
994 sccb_write(gspca_dev, 0x42, val & 0xbf);
997 static void setsatur(struct gspca_dev *gspca_dev)
999 struct sd *sd = (struct sd *) gspca_dev;
1000 u8 val1, val2, val3;
1001 static const u8 matrix[5][2] = {
1009 val1 = matrix[sd->satur][0];
1010 val2 = matrix[sd->satur][1];
1012 sccb_write(gspca_dev, 0x4f, val3); /* matrix coeff */
1013 sccb_write(gspca_dev, 0x50, val3);
1014 sccb_write(gspca_dev, 0x51, 0x00);
1015 sccb_write(gspca_dev, 0x52, val1);
1016 sccb_write(gspca_dev, 0x53, val2);
1017 sccb_write(gspca_dev, 0x54, val3);
1018 sccb_write(gspca_dev, 0x58, 0x1a); /* mtxs - coeff signs */
1020 val1 = sccb_read(gspca_dev, 0x41); /* com16 */
1021 sccb_write(gspca_dev, 0xff, 0x00);
1022 sccb_write(gspca_dev, 0x41, val1);
1025 static void setfreq(struct gspca_dev *gspca_dev)
1027 struct sd *sd = (struct sd *) gspca_dev;
1030 val = sccb_read(gspca_dev, 0x13); /* com8 */
1031 sccb_write(gspca_dev, 0xff, 0x00);
1032 if (sd->freq == 0) {
1033 sccb_write(gspca_dev, 0x13, val & 0xdf);
1036 sccb_write(gspca_dev, 0x13, val | 0x20);
1038 val = sccb_read(gspca_dev, 0x42); /* com17 */
1039 sccb_write(gspca_dev, 0xff, 0x00);
1044 sccb_write(gspca_dev, 0x42, val);
1047 /* this function is called at probe time */
1048 static int sd_config(struct gspca_dev *gspca_dev,
1049 const struct usb_device_id *id)
1051 struct sd *sd = (struct sd *) gspca_dev;
1054 cam = &gspca_dev->cam;
1056 cam->cam_mode = ov965x_mode;
1057 cam->nmodes = ARRAY_SIZE(ov965x_mode);
1059 sd->brightness = BRIGHTNESS_DEF;
1060 sd->contrast = CONTRAST_DEF;
1061 #if AUTOGAIN_DEF != 0
1062 sd->autogain = AUTOGAIN_DEF;
1063 gspca_dev->ctrl_inac |= (1 << EXPO_IDX);
1066 sd->exposure = EXPO_DEF;
1068 #if SHARPNESS_DEF != 0
1069 sd->sharpness = SHARPNESS_DEF;
1071 sd->satur = SATUR_DEF;
1072 sd->freq = FREQ_DEF;
1077 /* this function is called at probe and resume time */
1078 static int sd_init(struct gspca_dev *gspca_dev)
1083 reg_w(gspca_dev, 0xe7, 0x3a);
1084 reg_w(gspca_dev, 0xe0, 0x08);
1087 /* initialize the sensor address */
1088 reg_w(gspca_dev, OV534_REG_ADDRESS, 0x60);
1091 sccb_write(gspca_dev, 0x12, 0x80);
1094 /* probe the sensor */
1095 sccb_read(gspca_dev, 0x0a);
1096 sensor_id = sccb_read(gspca_dev, 0x0a) << 8;
1097 sccb_read(gspca_dev, 0x0b);
1098 sensor_id |= sccb_read(gspca_dev, 0x0b);
1099 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
1102 reg_w_array(gspca_dev, bridge_init,
1103 ARRAY_SIZE(bridge_init));
1104 sccb_w_array(gspca_dev, sensor_init,
1105 ARRAY_SIZE(sensor_init));
1106 reg_w_array(gspca_dev, bridge_init_2,
1107 ARRAY_SIZE(bridge_init_2));
1108 sccb_w_array(gspca_dev, sensor_init_2,
1109 ARRAY_SIZE(sensor_init_2));
1110 reg_w(gspca_dev, 0xe0, 0x00);
1111 reg_w(gspca_dev, 0xe0, 0x01);
1112 set_led(gspca_dev, 0);
1113 reg_w(gspca_dev, 0xe0, 0x00);
1115 return gspca_dev->usb_err;
1118 static int sd_start(struct gspca_dev *gspca_dev)
1120 switch (gspca_dev->curr_mode) {
1121 case QVGA_MODE: /* 320x240 */
1122 sccb_w_array(gspca_dev, sensor_start_1_vga,
1123 ARRAY_SIZE(sensor_start_1_vga));
1124 reg_w_array(gspca_dev, bridge_start_qvga,
1125 ARRAY_SIZE(bridge_start_qvga));
1126 sccb_w_array(gspca_dev, sensor_start_2_qvga,
1127 ARRAY_SIZE(sensor_start_2_qvga));
1129 case VGA_MODE: /* 640x480 */
1130 sccb_w_array(gspca_dev, sensor_start_1_vga,
1131 ARRAY_SIZE(sensor_start_1_vga));
1132 reg_w_array(gspca_dev, bridge_start_vga,
1133 ARRAY_SIZE(bridge_start_vga));
1134 sccb_w_array(gspca_dev, sensor_start_2_vga,
1135 ARRAY_SIZE(sensor_start_2_vga));
1137 case SVGA_MODE: /* 800x600 */
1138 sccb_w_array(gspca_dev, sensor_start_1_svga,
1139 ARRAY_SIZE(sensor_start_1_svga));
1140 reg_w_array(gspca_dev, bridge_start_svga,
1141 ARRAY_SIZE(bridge_start_svga));
1142 sccb_w_array(gspca_dev, sensor_start_2_svga,
1143 ARRAY_SIZE(sensor_start_2_svga));
1145 case XGA_MODE: /* 1024x768 */
1146 sccb_w_array(gspca_dev, sensor_start_1_xga,
1147 ARRAY_SIZE(sensor_start_1_xga));
1148 reg_w_array(gspca_dev, bridge_start_xga,
1149 ARRAY_SIZE(bridge_start_xga));
1150 sccb_w_array(gspca_dev, sensor_start_2_svga,
1151 ARRAY_SIZE(sensor_start_2_svga));
1154 /* case SXGA_MODE: * 1280x1024 */
1155 sccb_w_array(gspca_dev, sensor_start_1_sxga,
1156 ARRAY_SIZE(sensor_start_1_sxga));
1157 reg_w_array(gspca_dev, bridge_start_sxga,
1158 ARRAY_SIZE(bridge_start_sxga));
1159 sccb_w_array(gspca_dev, sensor_start_2_sxga,
1160 ARRAY_SIZE(sensor_start_2_sxga));
1164 setautogain(gspca_dev);
1165 setbrightness(gspca_dev);
1166 setcontrast(gspca_dev);
1167 setexposure(gspca_dev);
1168 setsharpness(gspca_dev);
1169 setsatur(gspca_dev);
1171 reg_w(gspca_dev, 0xe0, 0x00);
1172 reg_w(gspca_dev, 0xe0, 0x00);
1173 set_led(gspca_dev, 1);
1174 return gspca_dev->usb_err;
1177 static void sd_stopN(struct gspca_dev *gspca_dev)
1179 reg_w(gspca_dev, 0xe0, 0x01);
1180 set_led(gspca_dev, 0);
1181 reg_w(gspca_dev, 0xe0, 0x00);
1184 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1185 #define UVC_STREAM_EOH (1 << 7)
1186 #define UVC_STREAM_ERR (1 << 6)
1187 #define UVC_STREAM_STI (1 << 5)
1188 #define UVC_STREAM_RES (1 << 4)
1189 #define UVC_STREAM_SCR (1 << 3)
1190 #define UVC_STREAM_PTS (1 << 2)
1191 #define UVC_STREAM_EOF (1 << 1)
1192 #define UVC_STREAM_FID (1 << 0)
1194 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1197 struct sd *sd = (struct sd *) gspca_dev;
1200 int remaining_len = len;
1203 len = min(remaining_len, 2040);
1205 /* Payloads are prefixed with a UVC-style header. We
1206 consider a frame to start when the FID toggles, or the PTS
1207 changes. A frame ends when EOF is set, and we've received
1208 the correct number of bytes. */
1210 /* Verify UVC header. Header length is always 12 */
1211 if (data[0] != 12 || len < 12) {
1212 PDEBUG(D_PACK, "bad header");
1217 if (data[1] & UVC_STREAM_ERR) {
1218 PDEBUG(D_PACK, "payload error");
1222 /* Extract PTS and FID */
1223 if (!(data[1] & UVC_STREAM_PTS)) {
1224 PDEBUG(D_PACK, "PTS not present");
1227 this_pts = (data[5] << 24) | (data[4] << 16)
1228 | (data[3] << 8) | data[2];
1229 this_fid = data[1] & UVC_STREAM_FID;
1231 /* If PTS or FID has changed, start a new frame. */
1232 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
1233 if (gspca_dev->last_packet_type == INTER_PACKET)
1234 gspca_frame_add(gspca_dev, LAST_PACKET,
1236 sd->last_pts = this_pts;
1237 sd->last_fid = this_fid;
1238 gspca_frame_add(gspca_dev, FIRST_PACKET,
1239 data + 12, len - 12);
1240 /* If this packet is marked as EOF, end the frame */
1241 } else if (data[1] & UVC_STREAM_EOF) {
1243 gspca_frame_add(gspca_dev, LAST_PACKET,
1244 data + 12, len - 12);
1247 /* Add the data from this payload */
1248 gspca_frame_add(gspca_dev, INTER_PACKET,
1249 data + 12, len - 12);
1252 /* Done this payload */
1256 /* Discard data until a new frame starts. */
1257 gspca_dev->last_packet_type = DISCARD_PACKET;
1260 remaining_len -= len;
1262 } while (remaining_len > 0);
1266 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
1268 struct sd *sd = (struct sd *) gspca_dev;
1270 sd->brightness = val;
1271 if (gspca_dev->streaming)
1272 setbrightness(gspca_dev);
1273 return gspca_dev->usb_err;
1276 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1278 struct sd *sd = (struct sd *) gspca_dev;
1280 *val = sd->brightness;
1284 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
1286 struct sd *sd = (struct sd *) gspca_dev;
1289 if (gspca_dev->streaming)
1290 setcontrast(gspca_dev);
1291 return gspca_dev->usb_err;
1294 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
1296 struct sd *sd = (struct sd *) gspca_dev;
1298 *val = sd->contrast;
1302 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1304 struct sd *sd = (struct sd *) gspca_dev;
1308 if (gspca_dev->streaming) {
1310 gspca_dev->ctrl_inac |= (1 << EXPO_IDX);
1312 gspca_dev->ctrl_inac &= ~(1 << EXPO_IDX);
1313 setautogain(gspca_dev);
1315 return gspca_dev->usb_err;
1318 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1320 struct sd *sd = (struct sd *) gspca_dev;
1322 *val = sd->autogain;
1326 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1328 struct sd *sd = (struct sd *) gspca_dev;
1331 if (gspca_dev->streaming)
1332 setexposure(gspca_dev);
1333 return gspca_dev->usb_err;
1336 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1338 struct sd *sd = (struct sd *) gspca_dev;
1340 *val = sd->exposure;
1344 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
1346 struct sd *sd = (struct sd *) gspca_dev;
1348 sd->sharpness = val;
1349 if (gspca_dev->streaming)
1350 setsharpness(gspca_dev);
1351 return gspca_dev->usb_err;
1354 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
1356 struct sd *sd = (struct sd *) gspca_dev;
1358 *val = sd->sharpness;
1362 static int sd_setsatur(struct gspca_dev *gspca_dev, __s32 val)
1364 struct sd *sd = (struct sd *) gspca_dev;
1367 if (gspca_dev->streaming)
1368 setsatur(gspca_dev);
1369 return gspca_dev->usb_err;
1372 static int sd_getsatur(struct gspca_dev *gspca_dev, __s32 *val)
1374 struct sd *sd = (struct sd *) gspca_dev;
1379 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
1381 struct sd *sd = (struct sd *) gspca_dev;
1384 if (gspca_dev->streaming)
1386 return gspca_dev->usb_err;
1389 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
1391 struct sd *sd = (struct sd *) gspca_dev;
1397 static int sd_querymenu(struct gspca_dev *gspca_dev,
1398 struct v4l2_querymenu *menu)
1401 case V4L2_CID_POWER_LINE_FREQUENCY:
1402 switch (menu->index) {
1403 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1404 strcpy((char *) menu->name, "NoFliker");
1406 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1407 strcpy((char *) menu->name, "50 Hz");
1409 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
1410 strcpy((char *) menu->name, "60 Hz");
1418 /* sub-driver description */
1419 static const struct sd_desc sd_desc = {
1420 .name = MODULE_NAME,
1422 .nctrls = ARRAY_SIZE(sd_ctrls),
1423 .config = sd_config,
1427 .pkt_scan = sd_pkt_scan,
1428 .querymenu = sd_querymenu,
1431 /* -- module initialisation -- */
1432 static const struct usb_device_id device_table[] = {
1433 {USB_DEVICE(0x06f8, 0x3003)},
1437 MODULE_DEVICE_TABLE(usb, device_table);
1439 /* -- device connect -- */
1440 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1442 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1446 static struct usb_driver sd_driver = {
1447 .name = MODULE_NAME,
1448 .id_table = device_table,
1450 .disconnect = gspca_disconnect,
1452 .suspend = gspca_suspend,
1453 .resume = gspca_resume,
1457 /* -- module insert / remove -- */
1458 static int __init sd_mod_init(void)
1460 return usb_register(&sd_driver);
1463 static void __exit sd_mod_exit(void)
1465 usb_deregister(&sd_driver);
1468 module_init(sd_mod_init);
1469 module_exit(sd_mod_exit);