2 * ov534-ov9xxx gspca driver
4 * Copyright (C) 2009-2011 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.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #define MODULE_NAME "ov534_9"
29 #define OV534_REG_ADDRESS 0xf1 /* sensor address */
30 #define OV534_REG_SUBADDR 0xf2
31 #define OV534_REG_WRITE 0xf3
32 #define OV534_REG_READ 0xf4
33 #define OV534_REG_OPERATION 0xf5
34 #define OV534_REG_STATUS 0xf6
36 #define OV534_OP_WRITE_3 0x37
37 #define OV534_OP_WRITE_2 0x33
38 #define OV534_OP_READ_2 0xf9
40 #define CTRL_TIMEOUT 500
42 MODULE_AUTHOR("Jean-Francois Moine <moinejf@free.fr>");
43 MODULE_DESCRIPTION("GSPCA/OV534_9 USB Camera Driver");
44 MODULE_LICENSE("GPL");
46 /* specific webcam descriptor */
48 struct gspca_dev gspca_dev; /* !! must be the first item */
55 SENSOR_OV965x, /* ov9657 */
56 SENSOR_OV971x, /* ov9712 */
57 SENSOR_OV562x, /* ov5621 */
58 SENSOR_OV361x, /* ov3610 */
62 static const struct v4l2_pix_format ov965x_mode[] = {
64 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
66 .sizeimage = 320 * 240 * 3 / 8 + 590,
67 .colorspace = V4L2_COLORSPACE_JPEG},
69 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
71 .sizeimage = 640 * 480 * 3 / 8 + 590,
72 .colorspace = V4L2_COLORSPACE_JPEG},
74 {800, 600, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
76 .sizeimage = 800 * 600 * 3 / 8 + 590,
77 .colorspace = V4L2_COLORSPACE_JPEG},
79 {1024, 768, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
81 .sizeimage = 1024 * 768 * 3 / 8 + 590,
82 .colorspace = V4L2_COLORSPACE_JPEG},
84 {1280, 1024, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
86 .sizeimage = 1280 * 1024 * 3 / 8 + 590,
87 .colorspace = V4L2_COLORSPACE_JPEG},
90 static const struct v4l2_pix_format ov971x_mode[] = {
91 {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
93 .sizeimage = 640 * 480,
94 .colorspace = V4L2_COLORSPACE_SRGB
98 static const struct v4l2_pix_format ov562x_mode[] = {
99 {2592, 1680, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
100 .bytesperline = 2592,
101 .sizeimage = 2592 * 1680,
102 .colorspace = V4L2_COLORSPACE_SRGB
116 static const struct v4l2_pix_format ov361x_mode[] = {
117 {0x800, 0x600, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
118 .bytesperline = 0x800,
119 .sizeimage = 0x800 * 0x600,
120 .colorspace = V4L2_COLORSPACE_SRGB},
121 {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
122 .bytesperline = 1600,
123 .sizeimage = 1600 * 1200,
124 .colorspace = V4L2_COLORSPACE_SRGB},
125 {1024, 768, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
127 .sizeimage = 1024 * 768,
128 .colorspace = V4L2_COLORSPACE_SRGB},
129 {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
131 .sizeimage = 640 * 480,
132 .colorspace = V4L2_COLORSPACE_SRGB},
133 {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
135 .sizeimage = 320 * 240,
136 .colorspace = V4L2_COLORSPACE_SRGB},
137 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
139 .sizeimage = 160 * 120,
140 .colorspace = V4L2_COLORSPACE_SRGB}
143 static const u8 ov361x_start_2048[][2] = {
164 static const u8 ov361x_bridge_start_2048[][2] = {
181 static const u8 ov361x_start_1600[][2] = {
202 static const u8 ov361x_bridge_start_1600[][2] = {
203 {0xf1, 0x60}, /* Hsize[7:0] */
204 {0x88, 0x00}, /* Hsize[15:8] Write Only, can't read */
205 {0x89, 0x08}, /* Vsize[7:0] */
206 {0x8a, 0x00}, /* Vsize[15:8] Write Only, can't read */
207 {0x8b, 0x06}, /* for Iso */
208 {0x8c, 0x01}, /* RAW input */
210 {0x1c, 0x00}, /* RAW output, Iso transfer */
214 {0x1c, 0x0a}, /* turn off JPEG, Iso mode */
215 {0x1d, 0x2e}, /* for Iso */
219 static const u8 ov361x_start_1024[][2] = {
240 static const u8 ov361x_bridge_start_1024[][2] = {
241 {0xf1, 0x60}, /* Hsize[7:0] */
242 {0x88, 0x00}, /* Hsize[15:8] Write Only, can't read */
243 {0x89, 0x04}, /* Vsize[7:0] */
244 {0x8a, 0x00}, /* Vsize[15:8] Write Only, can't read */
245 {0x8b, 0x03}, /* for Iso */
246 {0x8c, 0x01}, /* RAW input */
248 {0x1c, 0x00}, /* RAW output, Iso transfer */
252 {0x1c, 0x0a}, /* turn off JPEG, Iso mode */
253 {0x1d, 0x2e}, /* for Iso */
257 static const u8 ov361x_start_640[][2] = {
279 static const u8 ov361x_bridge_start_640[][2] = {
280 {0xf1, 0x60}, /* Hsize[7:0]*/
281 {0x88, 0x00}, /* Hsize[15:8] Write Only, can't read */
282 {0x89, 0x04}, /* Vsize[7:0] */
283 {0x8a, 0x00}, /* Vsize[15:8] Write Only, can't read */
284 {0x8b, 0x03}, /* for Iso */
285 {0x8c, 0x01}, /* RAW input */
287 {0x1c, 0x00}, /* RAW output, Iso transfer */
291 {0x1c, 0x0a}, /* turn off JPEG, Iso mode */
292 {0x1d, 0x2e}, /* for Iso */
296 static const u8 ov361x_start_320[][2] = {
318 static const u8 ov361x_bridge_start_320[][2] = {
319 {0xf1, 0x60}, /* Hsize[7:0] */
320 {0x88, 0x00}, /* Hsize[15:8] Write Only, can't read */
321 {0x89, 0x04}, /* Vsize[7:0] */
322 {0x8a, 0x00}, /* Vsize[15:8] Write Only, can't read */
323 {0x8b, 0x03}, /* for Iso */
324 {0x8c, 0x01}, /* RAW input */
326 {0x1c, 0x00}, /* RAW output, Iso transfer; */
330 {0x1c, 0x0a}, /* turn off JPEG, Iso mode */
331 {0x1d, 0x2e}, /* for Iso */
335 static const u8 ov361x_start_160[][2] = {
357 static const u8 ov361x_bridge_start_160[][2] = {
358 {0xf1, 0x60}, /* Hsize[7:0] */
359 {0x88, 0x00}, /* Hsize[15:8] Write Only, can't read */
360 {0x89, 0x04}, /* Vsize[7:0] */
361 {0x8a, 0x00}, /* Vsize[15:8] Write Only, can't read */
362 {0x8b, 0x03}, /* for Iso */
363 {0x8c, 0x01}, /* RAW input */
365 {0x1c, 0x00}, /* RAW output, Iso transfer */
369 {0x1c, 0x0a}, /* turn off JPEG, Iso mode */
370 {0x1d, 0x2e}, /* for Iso */
374 static const u8 bridge_init[][2] = {
408 static const u8 ov965x_init[][2] = {
409 {0x12, 0x80}, /* com7 - SSCB reset */
410 {0x00, 0x00}, /* gain */
411 {0x01, 0x80}, /* blue */
412 {0x02, 0x80}, /* red */
413 {0x03, 0x1b}, /* vref */
414 {0x04, 0x03}, /* com1 - exposure low bits */
415 {0x0b, 0x57}, /* ver */
416 {0x0e, 0x61}, /* com5 */
417 {0x0f, 0x42}, /* com6 */
418 {0x11, 0x00}, /* clkrc */
419 {0x12, 0x02}, /* com7 - 15fps VGA YUYV */
420 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
421 {0x14, 0x28}, /* com9 */
422 {0x16, 0x24}, /* reg16 */
423 {0x17, 0x1d}, /* hstart*/
424 {0x18, 0xbd}, /* hstop */
425 {0x19, 0x01}, /* vstrt */
426 {0x1a, 0x81}, /* vstop*/
427 {0x1e, 0x04}, /* mvfp */
428 {0x24, 0x3c}, /* aew */
429 {0x25, 0x36}, /* aeb */
430 {0x26, 0x71}, /* vpt */
431 {0x27, 0x08}, /* bbias */
432 {0x28, 0x08}, /* gbbias */
433 {0x29, 0x15}, /* gr com */
434 {0x2a, 0x00}, /* exhch */
435 {0x2b, 0x00}, /* exhcl */
436 {0x2c, 0x08}, /* rbias */
437 {0x32, 0xff}, /* href */
438 {0x33, 0x00}, /* chlf */
439 {0x34, 0x3f}, /* aref1 */
440 {0x35, 0x00}, /* aref2 */
441 {0x36, 0xf8}, /* aref3 */
442 {0x38, 0x72}, /* adc2 */
443 {0x39, 0x57}, /* aref4 */
444 {0x3a, 0x80}, /* tslb - yuyv */
445 {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
446 {0x3d, 0x99}, /* com13 */
447 {0x3f, 0xc1}, /* edge */
448 {0x40, 0xc0}, /* com15 */
449 {0x41, 0x40}, /* com16 */
450 {0x42, 0xc0}, /* com17 */
451 {0x43, 0x0a}, /* rsvd */
462 {0x4f, 0x98}, /* matrix */
468 {0x58, 0x1a}, /* matrix coef sign */
469 {0x59, 0x85}, /* AWB control */
475 {0x5f, 0xf0}, /* AWB blue limit */
476 {0x60, 0xf0}, /* AWB red limit */
477 {0x61, 0xf0}, /* AWB green limit */
478 {0x62, 0x00}, /* lcc1 */
479 {0x63, 0x00}, /* lcc2 */
480 {0x64, 0x02}, /* lcc3 */
481 {0x65, 0x16}, /* lcc4 */
482 {0x66, 0x01}, /* lcc5 */
483 {0x69, 0x02}, /* hv */
484 {0x6b, 0x5a}, /* dbvl */
489 {0x70, 0x21}, /* dnsth */
491 {0x72, 0x00}, /* poidx */
492 {0x73, 0x01}, /* pckdv */
493 {0x74, 0x3a}, /* xindx */
494 {0x75, 0x35}, /* yindx */
497 {0x7a, 0x12}, /* gamma curve */
514 {0x8c, 0x89}, /* com19 */
515 {0x14, 0x28}, /* com9 */
518 {0x9d, 0x03}, /* lcc6 */
519 {0x9e, 0x04}, /* lcc7 */
522 {0xa1, 0x40}, /* aechm */
523 {0xa4, 0x50}, /* com21 */
524 {0xa5, 0x68}, /* com26 */
525 {0xa6, 0x4a}, /* AWB green */
526 {0xa8, 0xc1}, /* refa8 */
527 {0xa9, 0xef}, /* refa9 */
530 {0xac, 0x80}, /* black level control */
536 {0xb4, 0x20}, /* ctrlb4 */
540 {0xbc, 0x7f}, /* ADC channel offsets */
549 {0xc7, 0x80}, /* com24 */
555 {0x4f, 0x98}, /* matrix */
562 {0xff, 0x41}, /* read 41, write ff 00 */
563 {0x41, 0x40}, /* com16 */
565 {0xc5, 0x03}, /* 60 Hz banding filter */
566 {0x6a, 0x02}, /* 50 Hz banding filter */
568 {0x12, 0x62}, /* com7 - 30fps VGA YUV */
569 {0x36, 0xfa}, /* aref3 */
570 {0x69, 0x0a}, /* hv */
571 {0x8c, 0x89}, /* com22 */
572 {0x14, 0x28}, /* com9 */
574 {0x41, 0x40}, /* com16 */
581 {0x03, 0x12}, /* vref */
582 {0x17, 0x16}, /* hstart */
583 {0x18, 0x02}, /* hstop */
584 {0x19, 0x01}, /* vstrt */
585 {0x1a, 0x3d}, /* vstop */
586 {0x32, 0xff}, /* href */
590 static const u8 bridge_init_2[][2] = {
618 static const u8 ov965x_init_2[][2] = {
620 {0x1e, 0x04}, /* mvfp */
621 {0x13, 0xe0}, /* com8 */
622 {0x00, 0x00}, /* gain */
623 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
624 {0x11, 0x03}, /* clkrc */
625 {0x6b, 0x5a}, /* dblv */
631 {0xff, 0x42}, /* read 42, write ff 00 */
632 {0x42, 0xc0}, /* com17 */
634 {0xff, 0x42}, /* read 42, write ff 00 */
635 {0x42, 0xc1}, /* com17 */
638 {0xff, 0x42}, /* read 42, write ff 00 */
639 {0x42, 0xc1}, /* com17 */
641 {0x4f, 0x98}, /* matrix */
648 {0xff, 0x41}, /* read 41, write ff 00 */
649 {0x41, 0x40}, /* com16 */
655 {0x10, 0x25}, /* aech - exposure high bits */
656 {0xff, 0x13}, /* read 13, write ff 00 */
657 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
660 static const u8 ov971x_init[][2] = {
684 {0x98, 0x40}, /*{0x98, 0x00},*/
685 {0x99, 0xA0}, /*{0x99, 0x00},*/
686 {0x9a, 0x01}, /*{0x9a, 0x00},*/
688 {0x58, 0x78}, /*{0x58, 0xc8},*/
689 {0x59, 0x50}, /*{0x59, 0xa0},*/
694 {0xbd, 0x50}, /*{0xbd, 0xa0},*/
695 {0xbe, 0x78}, /*{0xbe, 0xc8},*/
718 static const u8 ov965x_start_1_vga[][2] = { /* same for qvga */
719 {0x12, 0x62}, /* com7 - 30fps VGA YUV */
720 {0x36, 0xfa}, /* aref3 */
721 {0x69, 0x0a}, /* hv */
722 {0x8c, 0x89}, /* com22 */
723 {0x14, 0x28}, /* com9 */
724 {0x3e, 0x0c}, /* com14 */
725 {0x41, 0x40}, /* com16 */
731 {0xc7, 0x80}, /* com24 */
732 {0x03, 0x12}, /* vref */
733 {0x17, 0x16}, /* hstart */
734 {0x18, 0x02}, /* hstop */
735 {0x19, 0x01}, /* vstrt */
736 {0x1a, 0x3d}, /* vstop */
737 {0x32, 0xff}, /* href */
741 static const u8 ov965x_start_1_svga[][2] = {
742 {0x12, 0x02}, /* com7 - YUYV - VGA 15 full resolution */
743 {0x36, 0xf8}, /* aref3 */
744 {0x69, 0x02}, /* hv */
745 {0x8c, 0x0d}, /* com22 */
746 {0x3e, 0x0c}, /* com14 */
747 {0x41, 0x40}, /* com16 */
753 {0xc7, 0x80}, /* com24 */
754 {0x03, 0x1b}, /* vref */
755 {0x17, 0x1d}, /* hstart */
756 {0x18, 0xbd}, /* hstop */
757 {0x19, 0x01}, /* vstrt */
758 {0x1a, 0x81}, /* vstop */
759 {0x32, 0xff}, /* href */
763 static const u8 ov965x_start_1_xga[][2] = {
764 {0x12, 0x02}, /* com7 */
765 {0x36, 0xf8}, /* aref3 */
766 {0x69, 0x02}, /* hv */
767 {0x8c, 0x89}, /* com22 */
768 {0x14, 0x28}, /* com9 */
769 {0x3e, 0x0c}, /* com14 */
770 {0x41, 0x40}, /* com16 */
776 {0xc7, 0x80}, /* com24 */
777 {0x03, 0x1b}, /* vref */
778 {0x17, 0x1d}, /* hstart */
779 {0x18, 0xbd}, /* hstop */
780 {0x19, 0x01}, /* vstrt */
781 {0x1a, 0x81}, /* vstop */
782 {0x32, 0xff}, /* href */
786 static const u8 ov965x_start_1_sxga[][2] = {
787 {0x12, 0x02}, /* com7 */
788 {0x36, 0xf8}, /* aref3 */
789 {0x69, 0x02}, /* hv */
790 {0x8c, 0x89}, /* com22 */
791 {0x14, 0x28}, /* com9 */
792 {0x3e, 0x0c}, /* com14 */
793 {0x41, 0x40}, /* com16 */
799 {0xc7, 0x80}, /* com24 */
800 {0x03, 0x1b}, /* vref */
801 {0x17, 0x1d}, /* hstart */
802 {0x18, 0x02}, /* hstop */
803 {0x19, 0x01}, /* vstrt */
804 {0x1a, 0x81}, /* vstop */
805 {0x32, 0xff}, /* href */
809 static const u8 bridge_start_qvga[][2] = {
837 static const u8 bridge_start_vga[][2] = {
864 static const u8 bridge_start_svga[][2] = {
891 static const u8 bridge_start_xga[][2] = {
918 static const u8 bridge_start_sxga[][2] = {
935 static const u8 ov965x_start_2_qvga[][2] = {
936 {0x3b, 0xe4}, /* com11 - night mode 1/4 frame rate */
937 {0x1e, 0x04}, /* mvfp */
938 {0x13, 0xe0}, /* com8 */
940 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
941 {0x11, 0x01}, /* clkrc */
942 {0x6b, 0x5a}, /* dblv */
943 {0x6a, 0x02}, /* 50 Hz banding filter */
944 {0xc5, 0x03}, /* 60 Hz banding filter */
945 {0xa2, 0x96}, /* bd50 */
946 {0xa3, 0x7d}, /* bd60 */
948 {0xff, 0x13}, /* read 13, write ff 00 */
950 {0x3a, 0x80}, /* tslb - yuyv */
953 static const u8 ov965x_start_2_vga[][2] = {
954 {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
955 {0x1e, 0x04}, /* mvfp */
956 {0x13, 0xe0}, /* com8 */
958 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
959 {0x11, 0x03}, /* clkrc */
960 {0x6b, 0x5a}, /* dblv */
961 {0x6a, 0x05}, /* 50 Hz banding filter */
962 {0xc5, 0x07}, /* 60 Hz banding filter */
963 {0xa2, 0x4b}, /* bd50 */
964 {0xa3, 0x3e}, /* bd60 */
966 {0x2d, 0x00}, /* advfl */
969 static const u8 ov965x_start_2_svga[][2] = { /* same for xga */
970 {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
971 {0x1e, 0x04}, /* mvfp */
972 {0x13, 0xe0}, /* com8 */
974 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
975 {0x11, 0x01}, /* clkrc */
976 {0x6b, 0x5a}, /* dblv */
977 {0x6a, 0x0c}, /* 50 Hz banding filter */
978 {0xc5, 0x0f}, /* 60 Hz banding filter */
979 {0xa2, 0x4e}, /* bd50 */
980 {0xa3, 0x41}, /* bd60 */
983 static const u8 ov965x_start_2_sxga[][2] = {
984 {0x13, 0xe0}, /* com8 */
986 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
987 {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
988 {0x1e, 0x04}, /* mvfp */
989 {0x11, 0x01}, /* clkrc */
990 {0x6b, 0x5a}, /* dblv */
991 {0x6a, 0x0c}, /* 50 Hz banding filter */
992 {0xc5, 0x0f}, /* 60 Hz banding filter */
993 {0xa2, 0x4e}, /* bd50 */
994 {0xa3, 0x41}, /* bd60 */
997 static const u8 ov562x_init[][2] = {
1013 static const u8 ov562x_init_2[][2] = {
1115 static void reg_w_i(struct gspca_dev *gspca_dev, u16 reg, u8 val)
1117 struct usb_device *udev = gspca_dev->dev;
1120 if (gspca_dev->usb_err < 0)
1122 gspca_dev->usb_buf[0] = val;
1123 ret = usb_control_msg(udev,
1124 usb_sndctrlpipe(udev, 0),
1126 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1127 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
1129 pr_err("reg_w failed %d\n", ret);
1130 gspca_dev->usb_err = ret;
1134 static void reg_w(struct gspca_dev *gspca_dev, u16 reg, u8 val)
1136 PDEBUG(D_USBO, "reg_w [%04x] = %02x", reg, val);
1137 reg_w_i(gspca_dev, reg, val);
1140 static u8 reg_r(struct gspca_dev *gspca_dev, u16 reg)
1142 struct usb_device *udev = gspca_dev->dev;
1145 if (gspca_dev->usb_err < 0)
1147 ret = usb_control_msg(udev,
1148 usb_rcvctrlpipe(udev, 0),
1150 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1151 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
1152 PDEBUG(D_USBI, "reg_r [%04x] -> %02x", reg, gspca_dev->usb_buf[0]);
1154 pr_err("reg_r err %d\n", ret);
1155 gspca_dev->usb_err = ret;
1158 return gspca_dev->usb_buf[0];
1161 static int sccb_check_status(struct gspca_dev *gspca_dev)
1166 for (i = 0; i < 5; i++) {
1168 data = reg_r(gspca_dev, OV534_REG_STATUS);
1178 PDEBUG(D_USBI|D_USBO,
1179 "sccb status 0x%02x, attempt %d/5",
1186 static void sccb_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
1188 PDEBUG(D_USBO, "sccb_write [%02x] = %02x", reg, val);
1189 reg_w_i(gspca_dev, OV534_REG_SUBADDR, reg);
1190 reg_w_i(gspca_dev, OV534_REG_WRITE, val);
1191 reg_w_i(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
1193 if (!sccb_check_status(gspca_dev))
1194 pr_err("sccb_write failed\n");
1197 static u8 sccb_read(struct gspca_dev *gspca_dev, u16 reg)
1199 reg_w(gspca_dev, OV534_REG_SUBADDR, reg);
1200 reg_w(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
1201 if (!sccb_check_status(gspca_dev))
1202 pr_err("sccb_read failed 1\n");
1204 reg_w(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
1205 if (!sccb_check_status(gspca_dev))
1206 pr_err("sccb_read failed 2\n");
1208 return reg_r(gspca_dev, OV534_REG_READ);
1211 /* output a bridge sequence (reg - val) */
1212 static void reg_w_array(struct gspca_dev *gspca_dev,
1213 const u8 (*data)[2], int len)
1215 while (--len >= 0) {
1216 reg_w(gspca_dev, (*data)[0], (*data)[1]);
1221 /* output a sensor sequence (reg - val) */
1222 static void sccb_w_array(struct gspca_dev *gspca_dev,
1223 const u8 (*data)[2], int len)
1225 while (--len >= 0) {
1226 if ((*data)[0] != 0xff) {
1227 sccb_write(gspca_dev, (*data)[0], (*data)[1]);
1229 sccb_read(gspca_dev, (*data)[1]);
1230 sccb_write(gspca_dev, 0xff, 0x00);
1236 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
1237 * (direction and output)? */
1238 static void set_led(struct gspca_dev *gspca_dev, int status)
1242 PDEBUG(D_CONF, "led status: %d", status);
1244 data = reg_r(gspca_dev, 0x21);
1246 reg_w(gspca_dev, 0x21, data);
1248 data = reg_r(gspca_dev, 0x23);
1254 reg_w(gspca_dev, 0x23, data);
1257 data = reg_r(gspca_dev, 0x21);
1259 reg_w(gspca_dev, 0x21, data);
1263 static void setbrightness(struct gspca_dev *gspca_dev, s32 brightness)
1265 struct sd *sd = (struct sd *) gspca_dev;
1269 if (sd->sensor == SENSOR_OV562x) {
1273 sccb_write(gspca_dev, 0x24, val);
1276 sccb_write(gspca_dev, 0x25, val);
1283 sccb_write(gspca_dev, 0x26, val);
1287 val = 15 - val; /* f .. 8 */
1289 val = val - 8; /* 0 .. 7 */
1290 sccb_write(gspca_dev, 0x55, /* brtn - brightness adjustment */
1295 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
1297 sccb_write(gspca_dev, 0x56, /* cnst1 - contrast 1 ctrl coeff */
1301 static void setautogain(struct gspca_dev *gspca_dev, s32 autogain)
1305 /*fixme: should adjust agc/awb/aec by different controls */
1306 val = sccb_read(gspca_dev, 0x13); /* com8 */
1307 sccb_write(gspca_dev, 0xff, 0x00);
1309 val |= 0x05; /* agc & aec */
1312 sccb_write(gspca_dev, 0x13, val);
1315 static void setexposure(struct gspca_dev *gspca_dev, s32 exposure)
1317 static const u8 expo[4] = {0x00, 0x25, 0x38, 0x5e};
1320 sccb_write(gspca_dev, 0x10, expo[exposure]); /* aec[9:2] */
1322 val = sccb_read(gspca_dev, 0x13); /* com8 */
1323 sccb_write(gspca_dev, 0xff, 0x00);
1324 sccb_write(gspca_dev, 0x13, val);
1326 val = sccb_read(gspca_dev, 0xa1); /* aech */
1327 sccb_write(gspca_dev, 0xff, 0x00);
1328 sccb_write(gspca_dev, 0xa1, val & 0xe0); /* aec[15:10] = 0 */
1331 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
1333 if (val < 0) { /* auto */
1334 val = sccb_read(gspca_dev, 0x42); /* com17 */
1335 sccb_write(gspca_dev, 0xff, 0x00);
1336 sccb_write(gspca_dev, 0x42, val | 0x40);
1337 /* Edge enhancement strength auto adjust */
1341 val = 1 << (val - 1);
1342 sccb_write(gspca_dev, 0x3f, /* edge - edge enhance. factor */
1344 val = sccb_read(gspca_dev, 0x42); /* com17 */
1345 sccb_write(gspca_dev, 0xff, 0x00);
1346 sccb_write(gspca_dev, 0x42, val & 0xbf);
1349 static void setsatur(struct gspca_dev *gspca_dev, s32 val)
1351 u8 val1, val2, val3;
1352 static const u8 matrix[5][2] = {
1360 val1 = matrix[val][0];
1361 val2 = matrix[val][1];
1363 sccb_write(gspca_dev, 0x4f, val3); /* matrix coeff */
1364 sccb_write(gspca_dev, 0x50, val3);
1365 sccb_write(gspca_dev, 0x51, 0x00);
1366 sccb_write(gspca_dev, 0x52, val1);
1367 sccb_write(gspca_dev, 0x53, val2);
1368 sccb_write(gspca_dev, 0x54, val3);
1369 sccb_write(gspca_dev, 0x58, 0x1a); /* mtxs - coeff signs */
1371 val1 = sccb_read(gspca_dev, 0x41); /* com16 */
1372 sccb_write(gspca_dev, 0xff, 0x00);
1373 sccb_write(gspca_dev, 0x41, val1);
1376 static void setlightfreq(struct gspca_dev *gspca_dev, s32 freq)
1380 val = sccb_read(gspca_dev, 0x13); /* com8 */
1381 sccb_write(gspca_dev, 0xff, 0x00);
1383 sccb_write(gspca_dev, 0x13, val & 0xdf);
1386 sccb_write(gspca_dev, 0x13, val | 0x20);
1388 val = sccb_read(gspca_dev, 0x42); /* com17 */
1389 sccb_write(gspca_dev, 0xff, 0x00);
1394 sccb_write(gspca_dev, 0x42, val);
1397 /* this function is called at probe time */
1398 static int sd_config(struct gspca_dev *gspca_dev,
1399 const struct usb_device_id *id)
1404 /* this function is called at probe and resume time */
1405 static int sd_init(struct gspca_dev *gspca_dev)
1407 struct sd *sd = (struct sd *) gspca_dev;
1411 reg_w(gspca_dev, 0xe7, 0x3a);
1412 reg_w(gspca_dev, 0xe0, 0x08);
1415 /* initialize the sensor address */
1416 reg_w(gspca_dev, OV534_REG_ADDRESS, 0x60);
1419 sccb_write(gspca_dev, 0x12, 0x80);
1422 /* probe the sensor */
1423 sccb_read(gspca_dev, 0x0a);
1424 sensor_id = sccb_read(gspca_dev, 0x0a) << 8;
1425 sccb_read(gspca_dev, 0x0b);
1426 sensor_id |= sccb_read(gspca_dev, 0x0b);
1427 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
1430 if ((sensor_id & 0xfff0) == 0x9650) {
1431 sd->sensor = SENSOR_OV965x;
1433 gspca_dev->cam.cam_mode = ov965x_mode;
1434 gspca_dev->cam.nmodes = ARRAY_SIZE(ov965x_mode);
1436 reg_w_array(gspca_dev, bridge_init,
1437 ARRAY_SIZE(bridge_init));
1438 sccb_w_array(gspca_dev, ov965x_init,
1439 ARRAY_SIZE(ov965x_init));
1440 reg_w_array(gspca_dev, bridge_init_2,
1441 ARRAY_SIZE(bridge_init_2));
1442 sccb_w_array(gspca_dev, ov965x_init_2,
1443 ARRAY_SIZE(ov965x_init_2));
1444 reg_w(gspca_dev, 0xe0, 0x00);
1445 reg_w(gspca_dev, 0xe0, 0x01);
1446 set_led(gspca_dev, 0);
1447 reg_w(gspca_dev, 0xe0, 0x00);
1448 } else if ((sensor_id & 0xfff0) == 0x9710) {
1452 sd->sensor = SENSOR_OV971x;
1454 gspca_dev->cam.cam_mode = ov971x_mode;
1455 gspca_dev->cam.nmodes = ARRAY_SIZE(ov971x_mode);
1457 gspca_dev->cam.bulk = 1;
1458 gspca_dev->cam.bulk_size = 16384;
1459 gspca_dev->cam.bulk_nurbs = 2;
1461 sccb_w_array(gspca_dev, ov971x_init,
1462 ARRAY_SIZE(ov971x_init));
1464 /* set video format on bridge processor */
1465 /* access bridge processor's video format registers at: 0x00 */
1466 reg_w(gspca_dev, 0x1c, 0x00);
1467 /*set register: 0x00 is 'RAW8', 0x40 is 'YUV422' (YUYV?)*/
1468 reg_w(gspca_dev, 0x1d, 0x00);
1470 /* Will W. specific stuff
1472 * output (0x1f) if first webcam
1473 * input (0x17) if 2nd or 3rd webcam */
1474 p = video_device_node_name(&gspca_dev->vdev);
1477 reg_w(gspca_dev, 0x56, 0x1f);
1479 reg_w(gspca_dev, 0x56, 0x17);
1480 } else if ((sensor_id & 0xfff0) == 0x5620) {
1481 sd->sensor = SENSOR_OV562x;
1482 gspca_dev->cam.cam_mode = ov562x_mode;
1483 gspca_dev->cam.nmodes = ARRAY_SIZE(ov562x_mode);
1485 reg_w_array(gspca_dev, ov562x_init,
1486 ARRAY_SIZE(ov562x_init));
1487 sccb_w_array(gspca_dev, ov562x_init_2,
1488 ARRAY_SIZE(ov562x_init_2));
1489 reg_w(gspca_dev, 0xe0, 0x00);
1490 } else if ((sensor_id & 0xfff0) == 0x3610) {
1491 sd->sensor = SENSOR_OV361x;
1492 gspca_dev->cam.cam_mode = ov361x_mode;
1493 gspca_dev->cam.nmodes = ARRAY_SIZE(ov361x_mode);
1494 reg_w(gspca_dev, 0xe7, 0x3a);
1495 reg_w(gspca_dev, 0xf1, 0x60);
1496 sccb_write(gspca_dev, 0x12, 0x80);
1498 pr_err("Unknown sensor %04x", sensor_id);
1502 return gspca_dev->usb_err;
1505 static int sd_start_ov361x(struct gspca_dev *gspca_dev)
1507 sccb_write(gspca_dev, 0x12, 0x80);
1509 switch (gspca_dev->curr_mode % (ov361x_last)) {
1511 reg_w_array(gspca_dev, ov361x_bridge_start_2048,
1512 ARRAY_SIZE(ov361x_bridge_start_2048));
1513 sccb_w_array(gspca_dev, ov361x_start_2048,
1514 ARRAY_SIZE(ov361x_start_2048));
1517 reg_w_array(gspca_dev, ov361x_bridge_start_1600,
1518 ARRAY_SIZE(ov361x_bridge_start_1600));
1519 sccb_w_array(gspca_dev, ov361x_start_1600,
1520 ARRAY_SIZE(ov361x_start_1600));
1523 reg_w_array(gspca_dev, ov361x_bridge_start_1024,
1524 ARRAY_SIZE(ov361x_bridge_start_1024));
1525 sccb_w_array(gspca_dev, ov361x_start_1024,
1526 ARRAY_SIZE(ov361x_start_1024));
1529 reg_w_array(gspca_dev, ov361x_bridge_start_640,
1530 ARRAY_SIZE(ov361x_bridge_start_640));
1531 sccb_w_array(gspca_dev, ov361x_start_640,
1532 ARRAY_SIZE(ov361x_start_640));
1535 reg_w_array(gspca_dev, ov361x_bridge_start_320,
1536 ARRAY_SIZE(ov361x_bridge_start_320));
1537 sccb_w_array(gspca_dev, ov361x_start_320,
1538 ARRAY_SIZE(ov361x_start_320));
1541 reg_w_array(gspca_dev, ov361x_bridge_start_160,
1542 ARRAY_SIZE(ov361x_bridge_start_160));
1543 sccb_w_array(gspca_dev, ov361x_start_160,
1544 ARRAY_SIZE(ov361x_start_160));
1547 reg_w(gspca_dev, 0xe0, 0x00); /* start transfer */
1549 return gspca_dev->usb_err;
1552 static int sd_start(struct gspca_dev *gspca_dev)
1554 struct sd *sd = (struct sd *) gspca_dev;
1556 if (sd->sensor == SENSOR_OV971x)
1557 return gspca_dev->usb_err;
1558 if (sd->sensor == SENSOR_OV562x)
1559 return gspca_dev->usb_err;
1560 if (sd->sensor == SENSOR_OV361x)
1561 return sd_start_ov361x(gspca_dev);
1563 switch (gspca_dev->curr_mode) {
1564 case QVGA_MODE: /* 320x240 */
1565 sccb_w_array(gspca_dev, ov965x_start_1_vga,
1566 ARRAY_SIZE(ov965x_start_1_vga));
1567 reg_w_array(gspca_dev, bridge_start_qvga,
1568 ARRAY_SIZE(bridge_start_qvga));
1569 sccb_w_array(gspca_dev, ov965x_start_2_qvga,
1570 ARRAY_SIZE(ov965x_start_2_qvga));
1572 case VGA_MODE: /* 640x480 */
1573 sccb_w_array(gspca_dev, ov965x_start_1_vga,
1574 ARRAY_SIZE(ov965x_start_1_vga));
1575 reg_w_array(gspca_dev, bridge_start_vga,
1576 ARRAY_SIZE(bridge_start_vga));
1577 sccb_w_array(gspca_dev, ov965x_start_2_vga,
1578 ARRAY_SIZE(ov965x_start_2_vga));
1580 case SVGA_MODE: /* 800x600 */
1581 sccb_w_array(gspca_dev, ov965x_start_1_svga,
1582 ARRAY_SIZE(ov965x_start_1_svga));
1583 reg_w_array(gspca_dev, bridge_start_svga,
1584 ARRAY_SIZE(bridge_start_svga));
1585 sccb_w_array(gspca_dev, ov965x_start_2_svga,
1586 ARRAY_SIZE(ov965x_start_2_svga));
1588 case XGA_MODE: /* 1024x768 */
1589 sccb_w_array(gspca_dev, ov965x_start_1_xga,
1590 ARRAY_SIZE(ov965x_start_1_xga));
1591 reg_w_array(gspca_dev, bridge_start_xga,
1592 ARRAY_SIZE(bridge_start_xga));
1593 sccb_w_array(gspca_dev, ov965x_start_2_svga,
1594 ARRAY_SIZE(ov965x_start_2_svga));
1597 /* case SXGA_MODE: * 1280x1024 */
1598 sccb_w_array(gspca_dev, ov965x_start_1_sxga,
1599 ARRAY_SIZE(ov965x_start_1_sxga));
1600 reg_w_array(gspca_dev, bridge_start_sxga,
1601 ARRAY_SIZE(bridge_start_sxga));
1602 sccb_w_array(gspca_dev, ov965x_start_2_sxga,
1603 ARRAY_SIZE(ov965x_start_2_sxga));
1607 reg_w(gspca_dev, 0xe0, 0x00);
1608 reg_w(gspca_dev, 0xe0, 0x00);
1609 set_led(gspca_dev, 1);
1610 return gspca_dev->usb_err;
1613 static void sd_stopN(struct gspca_dev *gspca_dev)
1615 if (((struct sd *)gspca_dev)->sensor == SENSOR_OV361x) {
1616 reg_w(gspca_dev, 0xe0, 0x01); /* stop transfer */
1617 /* reg_w(gspca_dev, 0x31, 0x09); */
1620 reg_w(gspca_dev, 0xe0, 0x01);
1621 set_led(gspca_dev, 0);
1622 reg_w(gspca_dev, 0xe0, 0x00);
1625 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1626 #define UVC_STREAM_EOH (1 << 7)
1627 #define UVC_STREAM_ERR (1 << 6)
1628 #define UVC_STREAM_STI (1 << 5)
1629 #define UVC_STREAM_RES (1 << 4)
1630 #define UVC_STREAM_SCR (1 << 3)
1631 #define UVC_STREAM_PTS (1 << 2)
1632 #define UVC_STREAM_EOF (1 << 1)
1633 #define UVC_STREAM_FID (1 << 0)
1635 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1638 struct sd *sd = (struct sd *) gspca_dev;
1641 int remaining_len = len;
1644 payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
1646 len = min(remaining_len, payload_len);
1648 /* Payloads are prefixed with a UVC-style header. We
1649 consider a frame to start when the FID toggles, or the PTS
1650 changes. A frame ends when EOF is set, and we've received
1651 the correct number of bytes. */
1653 /* Verify UVC header. Header length is always 12 */
1654 if (data[0] != 12 || len < 12) {
1655 PDEBUG(D_PACK, "bad header");
1660 if (data[1] & UVC_STREAM_ERR) {
1661 PDEBUG(D_PACK, "payload error");
1665 /* Extract PTS and FID */
1666 if (!(data[1] & UVC_STREAM_PTS)) {
1667 PDEBUG(D_PACK, "PTS not present");
1670 this_pts = (data[5] << 24) | (data[4] << 16)
1671 | (data[3] << 8) | data[2];
1672 this_fid = data[1] & UVC_STREAM_FID;
1674 /* If PTS or FID has changed, start a new frame. */
1675 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
1676 if (gspca_dev->last_packet_type == INTER_PACKET)
1677 gspca_frame_add(gspca_dev, LAST_PACKET,
1679 sd->last_pts = this_pts;
1680 sd->last_fid = this_fid;
1681 gspca_frame_add(gspca_dev, FIRST_PACKET,
1682 data + 12, len - 12);
1683 /* If this packet is marked as EOF, end the frame */
1684 } else if (data[1] & UVC_STREAM_EOF) {
1686 gspca_frame_add(gspca_dev, LAST_PACKET,
1687 data + 12, len - 12);
1690 /* Add the data from this payload */
1691 gspca_frame_add(gspca_dev, INTER_PACKET,
1692 data + 12, len - 12);
1695 /* Done this payload */
1699 /* Discard data until a new frame starts. */
1700 gspca_dev->last_packet_type = DISCARD_PACKET;
1703 remaining_len -= len;
1705 } while (remaining_len > 0);
1708 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
1710 struct gspca_dev *gspca_dev =
1711 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
1713 gspca_dev->usb_err = 0;
1715 if (!gspca_dev->streaming)
1719 case V4L2_CID_BRIGHTNESS:
1720 setbrightness(gspca_dev, ctrl->val);
1722 case V4L2_CID_CONTRAST:
1723 setcontrast(gspca_dev, ctrl->val);
1725 case V4L2_CID_SATURATION:
1726 setsatur(gspca_dev, ctrl->val);
1728 case V4L2_CID_POWER_LINE_FREQUENCY:
1729 setlightfreq(gspca_dev, ctrl->val);
1731 case V4L2_CID_SHARPNESS:
1732 setsharpness(gspca_dev, ctrl->val);
1734 case V4L2_CID_AUTOGAIN:
1736 setautogain(gspca_dev, ctrl->val);
1737 if (!ctrl->val && gspca_dev->exposure->is_new)
1738 setexposure(gspca_dev, gspca_dev->exposure->val);
1741 return gspca_dev->usb_err;
1744 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1745 .s_ctrl = sd_s_ctrl,
1748 static int sd_init_controls(struct gspca_dev *gspca_dev)
1750 struct sd *sd = (struct sd *)gspca_dev;
1751 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1753 if (sd->sensor == SENSOR_OV971x)
1755 if (sd->sensor == SENSOR_OV361x)
1757 gspca_dev->vdev.ctrl_handler = hdl;
1758 v4l2_ctrl_handler_init(hdl, 7);
1759 if (sd->sensor == SENSOR_OV562x) {
1760 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1761 V4L2_CID_BRIGHTNESS, -90, 90, 1, 0);
1763 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1764 V4L2_CID_BRIGHTNESS, 0, 15, 1, 7);
1765 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1766 V4L2_CID_CONTRAST, 0, 15, 1, 3);
1767 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1768 V4L2_CID_SATURATION, 0, 4, 1, 2);
1770 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1771 V4L2_CID_SHARPNESS, -1, 4, 1, -1);
1772 gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1773 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1774 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1775 V4L2_CID_EXPOSURE, 0, 3, 1, 0);
1776 v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
1777 V4L2_CID_POWER_LINE_FREQUENCY,
1778 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0, 0);
1779 v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
1783 pr_err("Could not initialize controls\n");
1789 /* sub-driver description */
1790 static const struct sd_desc sd_desc = {
1791 .name = MODULE_NAME,
1792 .config = sd_config,
1794 .init_controls = sd_init_controls,
1797 .pkt_scan = sd_pkt_scan,
1800 /* -- module initialisation -- */
1801 static const struct usb_device_id device_table[] = {
1802 {USB_DEVICE(0x05a9, 0x8065)},
1803 {USB_DEVICE(0x06f8, 0x3003)},
1804 {USB_DEVICE(0x05a9, 0x1550)},
1808 MODULE_DEVICE_TABLE(usb, device_table);
1810 /* -- device connect -- */
1811 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1813 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1817 static struct usb_driver sd_driver = {
1818 .name = MODULE_NAME,
1819 .id_table = device_table,
1821 .disconnect = gspca_disconnect,
1823 .suspend = gspca_suspend,
1824 .resume = gspca_resume,
1825 .reset_resume = gspca_resume,
1829 module_usb_driver(sd_driver);