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.
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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #define MODULE_NAME "ov534_9"
33 #define OV534_REG_ADDRESS 0xf1 /* sensor address */
34 #define OV534_REG_SUBADDR 0xf2
35 #define OV534_REG_WRITE 0xf3
36 #define OV534_REG_READ 0xf4
37 #define OV534_REG_OPERATION 0xf5
38 #define OV534_REG_STATUS 0xf6
40 #define OV534_OP_WRITE_3 0x37
41 #define OV534_OP_WRITE_2 0x33
42 #define OV534_OP_READ_2 0xf9
44 #define CTRL_TIMEOUT 500
46 MODULE_AUTHOR("Jean-Francois Moine <moinejf@free.fr>");
47 MODULE_DESCRIPTION("GSPCA/OV534_9 USB Camera Driver");
48 MODULE_LICENSE("GPL");
50 /* specific webcam descriptor */
52 struct gspca_dev gspca_dev; /* !! must be the first item */
59 SENSOR_OV965x, /* ov9657 */
60 SENSOR_OV971x, /* ov9712 */
61 SENSOR_OV562x, /* ov5621 */
62 SENSOR_OV361x, /* ov3610 */
66 static const struct v4l2_pix_format ov965x_mode[] = {
68 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
70 .sizeimage = 320 * 240 * 3 / 8 + 590,
71 .colorspace = V4L2_COLORSPACE_JPEG},
73 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
75 .sizeimage = 640 * 480 * 3 / 8 + 590,
76 .colorspace = V4L2_COLORSPACE_JPEG},
78 {800, 600, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
80 .sizeimage = 800 * 600 * 3 / 8 + 590,
81 .colorspace = V4L2_COLORSPACE_JPEG},
83 {1024, 768, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
85 .sizeimage = 1024 * 768 * 3 / 8 + 590,
86 .colorspace = V4L2_COLORSPACE_JPEG},
88 {1280, 1024, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
90 .sizeimage = 1280 * 1024 * 3 / 8 + 590,
91 .colorspace = V4L2_COLORSPACE_JPEG},
94 static const struct v4l2_pix_format ov971x_mode[] = {
95 {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
97 .sizeimage = 640 * 480,
98 .colorspace = V4L2_COLORSPACE_SRGB
102 static const struct v4l2_pix_format ov562x_mode[] = {
103 {2592, 1680, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
104 .bytesperline = 2592,
105 .sizeimage = 2592 * 1680,
106 .colorspace = V4L2_COLORSPACE_SRGB
120 static const struct v4l2_pix_format ov361x_mode[] = {
121 {0x800, 0x600, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
122 .bytesperline = 0x800,
123 .sizeimage = 0x800 * 0x600,
124 .colorspace = V4L2_COLORSPACE_SRGB},
125 {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
126 .bytesperline = 1600,
127 .sizeimage = 1600 * 1200,
128 .colorspace = V4L2_COLORSPACE_SRGB},
129 {1024, 768, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
131 .sizeimage = 1024 * 768,
132 .colorspace = V4L2_COLORSPACE_SRGB},
133 {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
135 .sizeimage = 640 * 480,
136 .colorspace = V4L2_COLORSPACE_SRGB},
137 {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
139 .sizeimage = 320 * 240,
140 .colorspace = V4L2_COLORSPACE_SRGB},
141 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
143 .sizeimage = 160 * 120,
144 .colorspace = V4L2_COLORSPACE_SRGB}
147 static const u8 ov361x_start_2048[][2] = {
168 static const u8 ov361x_bridge_start_2048[][2] = {
185 static const u8 ov361x_start_1600[][2] = {
206 static const u8 ov361x_bridge_start_1600[][2] = {
207 {0xf1, 0x60}, /* Hsize[7:0] */
208 {0x88, 0x00}, /* Hsize[15:8] Write Only, can't read */
209 {0x89, 0x08}, /* Vsize[7:0] */
210 {0x8a, 0x00}, /* Vsize[15:8] Write Only, can't read */
211 {0x8b, 0x06}, /* for Iso */
212 {0x8c, 0x01}, /* RAW input */
214 {0x1c, 0x00}, /* RAW output, Iso transfer */
218 {0x1c, 0x0a}, /* turn off JPEG, Iso mode */
219 {0x1d, 0x2e}, /* for Iso */
223 static const u8 ov361x_start_1024[][2] = {
244 static const u8 ov361x_bridge_start_1024[][2] = {
245 {0xf1, 0x60}, /* Hsize[7:0] */
246 {0x88, 0x00}, /* Hsize[15:8] Write Only, can't read */
247 {0x89, 0x04}, /* Vsize[7:0] */
248 {0x8a, 0x00}, /* Vsize[15:8] Write Only, can't read */
249 {0x8b, 0x03}, /* for Iso */
250 {0x8c, 0x01}, /* RAW input */
252 {0x1c, 0x00}, /* RAW output, Iso transfer */
256 {0x1c, 0x0a}, /* turn off JPEG, Iso mode */
257 {0x1d, 0x2e}, /* for Iso */
261 static const u8 ov361x_start_640[][2] = {
283 static const u8 ov361x_bridge_start_640[][2] = {
284 {0xf1, 0x60}, /* Hsize[7:0]*/
285 {0x88, 0x00}, /* Hsize[15:8] Write Only, can't read */
286 {0x89, 0x04}, /* Vsize[7:0] */
287 {0x8a, 0x00}, /* Vsize[15:8] Write Only, can't read */
288 {0x8b, 0x03}, /* for Iso */
289 {0x8c, 0x01}, /* RAW input */
291 {0x1c, 0x00}, /* RAW output, Iso transfer */
295 {0x1c, 0x0a}, /* turn off JPEG, Iso mode */
296 {0x1d, 0x2e}, /* for Iso */
300 static const u8 ov361x_start_320[][2] = {
322 static const u8 ov361x_bridge_start_320[][2] = {
323 {0xf1, 0x60}, /* Hsize[7:0] */
324 {0x88, 0x00}, /* Hsize[15:8] Write Only, can't read */
325 {0x89, 0x04}, /* Vsize[7:0] */
326 {0x8a, 0x00}, /* Vsize[15:8] Write Only, can't read */
327 {0x8b, 0x03}, /* for Iso */
328 {0x8c, 0x01}, /* RAW input */
330 {0x1c, 0x00}, /* RAW output, Iso transfer; */
334 {0x1c, 0x0a}, /* turn off JPEG, Iso mode */
335 {0x1d, 0x2e}, /* for Iso */
339 static const u8 ov361x_start_160[][2] = {
361 static const u8 ov361x_bridge_start_160[][2] = {
362 {0xf1, 0x60}, /* Hsize[7:0] */
363 {0x88, 0x00}, /* Hsize[15:8] Write Only, can't read */
364 {0x89, 0x04}, /* Vsize[7:0] */
365 {0x8a, 0x00}, /* Vsize[15:8] Write Only, can't read */
366 {0x8b, 0x03}, /* for Iso */
367 {0x8c, 0x01}, /* RAW input */
369 {0x1c, 0x00}, /* RAW output, Iso transfer */
373 {0x1c, 0x0a}, /* turn off JPEG, Iso mode */
374 {0x1d, 0x2e}, /* for Iso */
378 static const u8 bridge_init[][2] = {
412 static const u8 ov965x_init[][2] = {
413 {0x12, 0x80}, /* com7 - SSCB reset */
414 {0x00, 0x00}, /* gain */
415 {0x01, 0x80}, /* blue */
416 {0x02, 0x80}, /* red */
417 {0x03, 0x1b}, /* vref */
418 {0x04, 0x03}, /* com1 - exposure low bits */
419 {0x0b, 0x57}, /* ver */
420 {0x0e, 0x61}, /* com5 */
421 {0x0f, 0x42}, /* com6 */
422 {0x11, 0x00}, /* clkrc */
423 {0x12, 0x02}, /* com7 - 15fps VGA YUYV */
424 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
425 {0x14, 0x28}, /* com9 */
426 {0x16, 0x24}, /* reg16 */
427 {0x17, 0x1d}, /* hstart*/
428 {0x18, 0xbd}, /* hstop */
429 {0x19, 0x01}, /* vstrt */
430 {0x1a, 0x81}, /* vstop*/
431 {0x1e, 0x04}, /* mvfp */
432 {0x24, 0x3c}, /* aew */
433 {0x25, 0x36}, /* aeb */
434 {0x26, 0x71}, /* vpt */
435 {0x27, 0x08}, /* bbias */
436 {0x28, 0x08}, /* gbbias */
437 {0x29, 0x15}, /* gr com */
438 {0x2a, 0x00}, /* exhch */
439 {0x2b, 0x00}, /* exhcl */
440 {0x2c, 0x08}, /* rbias */
441 {0x32, 0xff}, /* href */
442 {0x33, 0x00}, /* chlf */
443 {0x34, 0x3f}, /* aref1 */
444 {0x35, 0x00}, /* aref2 */
445 {0x36, 0xf8}, /* aref3 */
446 {0x38, 0x72}, /* adc2 */
447 {0x39, 0x57}, /* aref4 */
448 {0x3a, 0x80}, /* tslb - yuyv */
449 {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
450 {0x3d, 0x99}, /* com13 */
451 {0x3f, 0xc1}, /* edge */
452 {0x40, 0xc0}, /* com15 */
453 {0x41, 0x40}, /* com16 */
454 {0x42, 0xc0}, /* com17 */
455 {0x43, 0x0a}, /* rsvd */
466 {0x4f, 0x98}, /* matrix */
472 {0x58, 0x1a}, /* matrix coef sign */
473 {0x59, 0x85}, /* AWB control */
479 {0x5f, 0xf0}, /* AWB blue limit */
480 {0x60, 0xf0}, /* AWB red limit */
481 {0x61, 0xf0}, /* AWB green limit */
482 {0x62, 0x00}, /* lcc1 */
483 {0x63, 0x00}, /* lcc2 */
484 {0x64, 0x02}, /* lcc3 */
485 {0x65, 0x16}, /* lcc4 */
486 {0x66, 0x01}, /* lcc5 */
487 {0x69, 0x02}, /* hv */
488 {0x6b, 0x5a}, /* dbvl */
493 {0x70, 0x21}, /* dnsth */
495 {0x72, 0x00}, /* poidx */
496 {0x73, 0x01}, /* pckdv */
497 {0x74, 0x3a}, /* xindx */
498 {0x75, 0x35}, /* yindx */
501 {0x7a, 0x12}, /* gamma curve */
518 {0x8c, 0x89}, /* com19 */
519 {0x14, 0x28}, /* com9 */
522 {0x9d, 0x03}, /* lcc6 */
523 {0x9e, 0x04}, /* lcc7 */
526 {0xa1, 0x40}, /* aechm */
527 {0xa4, 0x50}, /* com21 */
528 {0xa5, 0x68}, /* com26 */
529 {0xa6, 0x4a}, /* AWB green */
530 {0xa8, 0xc1}, /* refa8 */
531 {0xa9, 0xef}, /* refa9 */
534 {0xac, 0x80}, /* black level control */
540 {0xb4, 0x20}, /* ctrlb4 */
544 {0xbc, 0x7f}, /* ADC channel offsets */
553 {0xc7, 0x80}, /* com24 */
559 {0x4f, 0x98}, /* matrix */
566 {0xff, 0x41}, /* read 41, write ff 00 */
567 {0x41, 0x40}, /* com16 */
569 {0xc5, 0x03}, /* 60 Hz banding filter */
570 {0x6a, 0x02}, /* 50 Hz banding filter */
572 {0x12, 0x62}, /* com7 - 30fps VGA YUV */
573 {0x36, 0xfa}, /* aref3 */
574 {0x69, 0x0a}, /* hv */
575 {0x8c, 0x89}, /* com22 */
576 {0x14, 0x28}, /* com9 */
578 {0x41, 0x40}, /* com16 */
585 {0x03, 0x12}, /* vref */
586 {0x17, 0x16}, /* hstart */
587 {0x18, 0x02}, /* hstop */
588 {0x19, 0x01}, /* vstrt */
589 {0x1a, 0x3d}, /* vstop */
590 {0x32, 0xff}, /* href */
594 static const u8 bridge_init_2[][2] = {
622 static const u8 ov965x_init_2[][2] = {
624 {0x1e, 0x04}, /* mvfp */
625 {0x13, 0xe0}, /* com8 */
626 {0x00, 0x00}, /* gain */
627 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
628 {0x11, 0x03}, /* clkrc */
629 {0x6b, 0x5a}, /* dblv */
635 {0xff, 0x42}, /* read 42, write ff 00 */
636 {0x42, 0xc0}, /* com17 */
638 {0xff, 0x42}, /* read 42, write ff 00 */
639 {0x42, 0xc1}, /* com17 */
642 {0xff, 0x42}, /* read 42, write ff 00 */
643 {0x42, 0xc1}, /* com17 */
645 {0x4f, 0x98}, /* matrix */
652 {0xff, 0x41}, /* read 41, write ff 00 */
653 {0x41, 0x40}, /* com16 */
659 {0x10, 0x25}, /* aech - exposure high bits */
660 {0xff, 0x13}, /* read 13, write ff 00 */
661 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
664 static const u8 ov971x_init[][2] = {
688 {0x98, 0x40}, /*{0x98, 0x00},*/
689 {0x99, 0xA0}, /*{0x99, 0x00},*/
690 {0x9a, 0x01}, /*{0x9a, 0x00},*/
692 {0x58, 0x78}, /*{0x58, 0xc8},*/
693 {0x59, 0x50}, /*{0x59, 0xa0},*/
698 {0xbd, 0x50}, /*{0xbd, 0xa0},*/
699 {0xbe, 0x78}, /*{0xbe, 0xc8},*/
722 static const u8 ov965x_start_1_vga[][2] = { /* same for qvga */
723 {0x12, 0x62}, /* com7 - 30fps VGA YUV */
724 {0x36, 0xfa}, /* aref3 */
725 {0x69, 0x0a}, /* hv */
726 {0x8c, 0x89}, /* com22 */
727 {0x14, 0x28}, /* com9 */
728 {0x3e, 0x0c}, /* com14 */
729 {0x41, 0x40}, /* com16 */
735 {0xc7, 0x80}, /* com24 */
736 {0x03, 0x12}, /* vref */
737 {0x17, 0x16}, /* hstart */
738 {0x18, 0x02}, /* hstop */
739 {0x19, 0x01}, /* vstrt */
740 {0x1a, 0x3d}, /* vstop */
741 {0x32, 0xff}, /* href */
745 static const u8 ov965x_start_1_svga[][2] = {
746 {0x12, 0x02}, /* com7 - YUYV - VGA 15 full resolution */
747 {0x36, 0xf8}, /* aref3 */
748 {0x69, 0x02}, /* hv */
749 {0x8c, 0x0d}, /* com22 */
750 {0x3e, 0x0c}, /* com14 */
751 {0x41, 0x40}, /* com16 */
757 {0xc7, 0x80}, /* com24 */
758 {0x03, 0x1b}, /* vref */
759 {0x17, 0x1d}, /* hstart */
760 {0x18, 0xbd}, /* hstop */
761 {0x19, 0x01}, /* vstrt */
762 {0x1a, 0x81}, /* vstop */
763 {0x32, 0xff}, /* href */
767 static const u8 ov965x_start_1_xga[][2] = {
768 {0x12, 0x02}, /* com7 */
769 {0x36, 0xf8}, /* aref3 */
770 {0x69, 0x02}, /* hv */
771 {0x8c, 0x89}, /* com22 */
772 {0x14, 0x28}, /* com9 */
773 {0x3e, 0x0c}, /* com14 */
774 {0x41, 0x40}, /* com16 */
780 {0xc7, 0x80}, /* com24 */
781 {0x03, 0x1b}, /* vref */
782 {0x17, 0x1d}, /* hstart */
783 {0x18, 0xbd}, /* hstop */
784 {0x19, 0x01}, /* vstrt */
785 {0x1a, 0x81}, /* vstop */
786 {0x32, 0xff}, /* href */
790 static const u8 ov965x_start_1_sxga[][2] = {
791 {0x12, 0x02}, /* com7 */
792 {0x36, 0xf8}, /* aref3 */
793 {0x69, 0x02}, /* hv */
794 {0x8c, 0x89}, /* com22 */
795 {0x14, 0x28}, /* com9 */
796 {0x3e, 0x0c}, /* com14 */
797 {0x41, 0x40}, /* com16 */
803 {0xc7, 0x80}, /* com24 */
804 {0x03, 0x1b}, /* vref */
805 {0x17, 0x1d}, /* hstart */
806 {0x18, 0x02}, /* hstop */
807 {0x19, 0x01}, /* vstrt */
808 {0x1a, 0x81}, /* vstop */
809 {0x32, 0xff}, /* href */
813 static const u8 bridge_start_qvga[][2] = {
841 static const u8 bridge_start_vga[][2] = {
868 static const u8 bridge_start_svga[][2] = {
895 static const u8 bridge_start_xga[][2] = {
922 static const u8 bridge_start_sxga[][2] = {
939 static const u8 ov965x_start_2_qvga[][2] = {
940 {0x3b, 0xe4}, /* com11 - night mode 1/4 frame rate */
941 {0x1e, 0x04}, /* mvfp */
942 {0x13, 0xe0}, /* com8 */
944 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
945 {0x11, 0x01}, /* clkrc */
946 {0x6b, 0x5a}, /* dblv */
947 {0x6a, 0x02}, /* 50 Hz banding filter */
948 {0xc5, 0x03}, /* 60 Hz banding filter */
949 {0xa2, 0x96}, /* bd50 */
950 {0xa3, 0x7d}, /* bd60 */
952 {0xff, 0x13}, /* read 13, write ff 00 */
954 {0x3a, 0x80}, /* tslb - yuyv */
957 static const u8 ov965x_start_2_vga[][2] = {
958 {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
959 {0x1e, 0x04}, /* mvfp */
960 {0x13, 0xe0}, /* com8 */
962 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
963 {0x11, 0x03}, /* clkrc */
964 {0x6b, 0x5a}, /* dblv */
965 {0x6a, 0x05}, /* 50 Hz banding filter */
966 {0xc5, 0x07}, /* 60 Hz banding filter */
967 {0xa2, 0x4b}, /* bd50 */
968 {0xa3, 0x3e}, /* bd60 */
970 {0x2d, 0x00}, /* advfl */
973 static const u8 ov965x_start_2_svga[][2] = { /* same for xga */
974 {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
975 {0x1e, 0x04}, /* mvfp */
976 {0x13, 0xe0}, /* com8 */
978 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
979 {0x11, 0x01}, /* clkrc */
980 {0x6b, 0x5a}, /* dblv */
981 {0x6a, 0x0c}, /* 50 Hz banding filter */
982 {0xc5, 0x0f}, /* 60 Hz banding filter */
983 {0xa2, 0x4e}, /* bd50 */
984 {0xa3, 0x41}, /* bd60 */
987 static const u8 ov965x_start_2_sxga[][2] = {
988 {0x13, 0xe0}, /* com8 */
990 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
991 {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
992 {0x1e, 0x04}, /* mvfp */
993 {0x11, 0x01}, /* clkrc */
994 {0x6b, 0x5a}, /* dblv */
995 {0x6a, 0x0c}, /* 50 Hz banding filter */
996 {0xc5, 0x0f}, /* 60 Hz banding filter */
997 {0xa2, 0x4e}, /* bd50 */
998 {0xa3, 0x41}, /* bd60 */
1001 static const u8 ov562x_init[][2] = {
1017 static const u8 ov562x_init_2[][2] = {
1119 static void reg_w_i(struct gspca_dev *gspca_dev, u16 reg, u8 val)
1121 struct usb_device *udev = gspca_dev->dev;
1124 if (gspca_dev->usb_err < 0)
1126 gspca_dev->usb_buf[0] = val;
1127 ret = usb_control_msg(udev,
1128 usb_sndctrlpipe(udev, 0),
1130 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1131 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
1133 pr_err("reg_w failed %d\n", ret);
1134 gspca_dev->usb_err = ret;
1138 static void reg_w(struct gspca_dev *gspca_dev, u16 reg, u8 val)
1140 PDEBUG(D_USBO, "reg_w [%04x] = %02x", reg, val);
1141 reg_w_i(gspca_dev, reg, val);
1144 static u8 reg_r(struct gspca_dev *gspca_dev, u16 reg)
1146 struct usb_device *udev = gspca_dev->dev;
1149 if (gspca_dev->usb_err < 0)
1151 ret = usb_control_msg(udev,
1152 usb_rcvctrlpipe(udev, 0),
1154 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1155 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
1156 PDEBUG(D_USBI, "reg_r [%04x] -> %02x", reg, gspca_dev->usb_buf[0]);
1158 pr_err("reg_r err %d\n", ret);
1159 gspca_dev->usb_err = ret;
1161 return gspca_dev->usb_buf[0];
1164 static int sccb_check_status(struct gspca_dev *gspca_dev)
1169 for (i = 0; i < 5; i++) {
1171 data = reg_r(gspca_dev, OV534_REG_STATUS);
1181 PDEBUG(D_USBI|D_USBO,
1182 "sccb status 0x%02x, attempt %d/5",
1189 static void sccb_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
1191 PDEBUG(D_USBO, "sccb_write [%02x] = %02x", reg, val);
1192 reg_w_i(gspca_dev, OV534_REG_SUBADDR, reg);
1193 reg_w_i(gspca_dev, OV534_REG_WRITE, val);
1194 reg_w_i(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
1196 if (!sccb_check_status(gspca_dev))
1197 pr_err("sccb_write failed\n");
1200 static u8 sccb_read(struct gspca_dev *gspca_dev, u16 reg)
1202 reg_w(gspca_dev, OV534_REG_SUBADDR, reg);
1203 reg_w(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
1204 if (!sccb_check_status(gspca_dev))
1205 pr_err("sccb_read failed 1\n");
1207 reg_w(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
1208 if (!sccb_check_status(gspca_dev))
1209 pr_err("sccb_read failed 2\n");
1211 return reg_r(gspca_dev, OV534_REG_READ);
1214 /* output a bridge sequence (reg - val) */
1215 static void reg_w_array(struct gspca_dev *gspca_dev,
1216 const u8 (*data)[2], int len)
1218 while (--len >= 0) {
1219 reg_w(gspca_dev, (*data)[0], (*data)[1]);
1224 /* output a sensor sequence (reg - val) */
1225 static void sccb_w_array(struct gspca_dev *gspca_dev,
1226 const u8 (*data)[2], int len)
1228 while (--len >= 0) {
1229 if ((*data)[0] != 0xff) {
1230 sccb_write(gspca_dev, (*data)[0], (*data)[1]);
1232 sccb_read(gspca_dev, (*data)[1]);
1233 sccb_write(gspca_dev, 0xff, 0x00);
1239 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
1240 * (direction and output)? */
1241 static void set_led(struct gspca_dev *gspca_dev, int status)
1245 PDEBUG(D_CONF, "led status: %d", status);
1247 data = reg_r(gspca_dev, 0x21);
1249 reg_w(gspca_dev, 0x21, data);
1251 data = reg_r(gspca_dev, 0x23);
1257 reg_w(gspca_dev, 0x23, data);
1260 data = reg_r(gspca_dev, 0x21);
1262 reg_w(gspca_dev, 0x21, data);
1266 static void setbrightness(struct gspca_dev *gspca_dev, s32 brightness)
1268 struct sd *sd = (struct sd *) gspca_dev;
1272 if (sd->sensor == SENSOR_OV562x) {
1276 sccb_write(gspca_dev, 0x24, val);
1279 sccb_write(gspca_dev, 0x25, val);
1286 sccb_write(gspca_dev, 0x26, val);
1290 val = 15 - val; /* f .. 8 */
1292 val = val - 8; /* 0 .. 7 */
1293 sccb_write(gspca_dev, 0x55, /* brtn - brightness adjustment */
1298 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
1300 sccb_write(gspca_dev, 0x56, /* cnst1 - contrast 1 ctrl coeff */
1304 static void setautogain(struct gspca_dev *gspca_dev, s32 autogain)
1308 /*fixme: should adjust agc/awb/aec by different controls */
1309 val = sccb_read(gspca_dev, 0x13); /* com8 */
1310 sccb_write(gspca_dev, 0xff, 0x00);
1312 val |= 0x05; /* agc & aec */
1315 sccb_write(gspca_dev, 0x13, val);
1318 static void setexposure(struct gspca_dev *gspca_dev, s32 exposure)
1320 static const u8 expo[4] = {0x00, 0x25, 0x38, 0x5e};
1323 sccb_write(gspca_dev, 0x10, expo[exposure]); /* aec[9:2] */
1325 val = sccb_read(gspca_dev, 0x13); /* com8 */
1326 sccb_write(gspca_dev, 0xff, 0x00);
1327 sccb_write(gspca_dev, 0x13, val);
1329 val = sccb_read(gspca_dev, 0xa1); /* aech */
1330 sccb_write(gspca_dev, 0xff, 0x00);
1331 sccb_write(gspca_dev, 0xa1, val & 0xe0); /* aec[15:10] = 0 */
1334 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
1336 if (val < 0) { /* auto */
1337 val = sccb_read(gspca_dev, 0x42); /* com17 */
1338 sccb_write(gspca_dev, 0xff, 0x00);
1339 sccb_write(gspca_dev, 0x42, val | 0x40);
1340 /* Edge enhancement strength auto adjust */
1344 val = 1 << (val - 1);
1345 sccb_write(gspca_dev, 0x3f, /* edge - edge enhance. factor */
1347 val = sccb_read(gspca_dev, 0x42); /* com17 */
1348 sccb_write(gspca_dev, 0xff, 0x00);
1349 sccb_write(gspca_dev, 0x42, val & 0xbf);
1352 static void setsatur(struct gspca_dev *gspca_dev, s32 val)
1354 u8 val1, val2, val3;
1355 static const u8 matrix[5][2] = {
1363 val1 = matrix[val][0];
1364 val2 = matrix[val][1];
1366 sccb_write(gspca_dev, 0x4f, val3); /* matrix coeff */
1367 sccb_write(gspca_dev, 0x50, val3);
1368 sccb_write(gspca_dev, 0x51, 0x00);
1369 sccb_write(gspca_dev, 0x52, val1);
1370 sccb_write(gspca_dev, 0x53, val2);
1371 sccb_write(gspca_dev, 0x54, val3);
1372 sccb_write(gspca_dev, 0x58, 0x1a); /* mtxs - coeff signs */
1374 val1 = sccb_read(gspca_dev, 0x41); /* com16 */
1375 sccb_write(gspca_dev, 0xff, 0x00);
1376 sccb_write(gspca_dev, 0x41, val1);
1379 static void setlightfreq(struct gspca_dev *gspca_dev, s32 freq)
1383 val = sccb_read(gspca_dev, 0x13); /* com8 */
1384 sccb_write(gspca_dev, 0xff, 0x00);
1386 sccb_write(gspca_dev, 0x13, val & 0xdf);
1389 sccb_write(gspca_dev, 0x13, val | 0x20);
1391 val = sccb_read(gspca_dev, 0x42); /* com17 */
1392 sccb_write(gspca_dev, 0xff, 0x00);
1397 sccb_write(gspca_dev, 0x42, val);
1400 /* this function is called at probe time */
1401 static int sd_config(struct gspca_dev *gspca_dev,
1402 const struct usb_device_id *id)
1407 /* this function is called at probe and resume time */
1408 static int sd_init(struct gspca_dev *gspca_dev)
1410 struct sd *sd = (struct sd *) gspca_dev;
1414 reg_w(gspca_dev, 0xe7, 0x3a);
1415 reg_w(gspca_dev, 0xe0, 0x08);
1418 /* initialize the sensor address */
1419 reg_w(gspca_dev, OV534_REG_ADDRESS, 0x60);
1422 sccb_write(gspca_dev, 0x12, 0x80);
1425 /* probe the sensor */
1426 sccb_read(gspca_dev, 0x0a);
1427 sensor_id = sccb_read(gspca_dev, 0x0a) << 8;
1428 sccb_read(gspca_dev, 0x0b);
1429 sensor_id |= sccb_read(gspca_dev, 0x0b);
1430 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
1433 if ((sensor_id & 0xfff0) == 0x9650) {
1434 sd->sensor = SENSOR_OV965x;
1436 gspca_dev->cam.cam_mode = ov965x_mode;
1437 gspca_dev->cam.nmodes = ARRAY_SIZE(ov965x_mode);
1439 reg_w_array(gspca_dev, bridge_init,
1440 ARRAY_SIZE(bridge_init));
1441 sccb_w_array(gspca_dev, ov965x_init,
1442 ARRAY_SIZE(ov965x_init));
1443 reg_w_array(gspca_dev, bridge_init_2,
1444 ARRAY_SIZE(bridge_init_2));
1445 sccb_w_array(gspca_dev, ov965x_init_2,
1446 ARRAY_SIZE(ov965x_init_2));
1447 reg_w(gspca_dev, 0xe0, 0x00);
1448 reg_w(gspca_dev, 0xe0, 0x01);
1449 set_led(gspca_dev, 0);
1450 reg_w(gspca_dev, 0xe0, 0x00);
1451 } else if ((sensor_id & 0xfff0) == 0x9710) {
1455 sd->sensor = SENSOR_OV971x;
1457 gspca_dev->cam.cam_mode = ov971x_mode;
1458 gspca_dev->cam.nmodes = ARRAY_SIZE(ov971x_mode);
1460 gspca_dev->cam.bulk = 1;
1461 gspca_dev->cam.bulk_size = 16384;
1462 gspca_dev->cam.bulk_nurbs = 2;
1464 sccb_w_array(gspca_dev, ov971x_init,
1465 ARRAY_SIZE(ov971x_init));
1467 /* set video format on bridge processor */
1468 /* access bridge processor's video format registers at: 0x00 */
1469 reg_w(gspca_dev, 0x1c, 0x00);
1470 /*set register: 0x00 is 'RAW8', 0x40 is 'YUV422' (YUYV?)*/
1471 reg_w(gspca_dev, 0x1d, 0x00);
1473 /* Will W. specific stuff
1475 * output (0x1f) if first webcam
1476 * input (0x17) if 2nd or 3rd webcam */
1477 p = video_device_node_name(&gspca_dev->vdev);
1480 reg_w(gspca_dev, 0x56, 0x1f);
1482 reg_w(gspca_dev, 0x56, 0x17);
1483 } else if ((sensor_id & 0xfff0) == 0x5620) {
1484 sd->sensor = SENSOR_OV562x;
1485 gspca_dev->cam.cam_mode = ov562x_mode;
1486 gspca_dev->cam.nmodes = ARRAY_SIZE(ov562x_mode);
1488 reg_w_array(gspca_dev, ov562x_init,
1489 ARRAY_SIZE(ov562x_init));
1490 sccb_w_array(gspca_dev, ov562x_init_2,
1491 ARRAY_SIZE(ov562x_init_2));
1492 reg_w(gspca_dev, 0xe0, 0x00);
1493 } else if ((sensor_id & 0xfff0) == 0x3610) {
1494 sd->sensor = SENSOR_OV361x;
1495 gspca_dev->cam.cam_mode = ov361x_mode;
1496 gspca_dev->cam.nmodes = ARRAY_SIZE(ov361x_mode);
1497 reg_w(gspca_dev, 0xe7, 0x3a);
1498 reg_w(gspca_dev, 0xf1, 0x60);
1499 sccb_write(gspca_dev, 0x12, 0x80);
1501 pr_err("Unknown sensor %04x", sensor_id);
1505 return gspca_dev->usb_err;
1508 static int sd_start_ov361x(struct gspca_dev *gspca_dev)
1510 sccb_write(gspca_dev, 0x12, 0x80);
1512 switch (gspca_dev->curr_mode % (ov361x_last)) {
1514 reg_w_array(gspca_dev, ov361x_bridge_start_2048,
1515 ARRAY_SIZE(ov361x_bridge_start_2048));
1516 sccb_w_array(gspca_dev, ov361x_start_2048,
1517 ARRAY_SIZE(ov361x_start_2048));
1520 reg_w_array(gspca_dev, ov361x_bridge_start_1600,
1521 ARRAY_SIZE(ov361x_bridge_start_1600));
1522 sccb_w_array(gspca_dev, ov361x_start_1600,
1523 ARRAY_SIZE(ov361x_start_1600));
1526 reg_w_array(gspca_dev, ov361x_bridge_start_1024,
1527 ARRAY_SIZE(ov361x_bridge_start_1024));
1528 sccb_w_array(gspca_dev, ov361x_start_1024,
1529 ARRAY_SIZE(ov361x_start_1024));
1532 reg_w_array(gspca_dev, ov361x_bridge_start_640,
1533 ARRAY_SIZE(ov361x_bridge_start_640));
1534 sccb_w_array(gspca_dev, ov361x_start_640,
1535 ARRAY_SIZE(ov361x_start_640));
1538 reg_w_array(gspca_dev, ov361x_bridge_start_320,
1539 ARRAY_SIZE(ov361x_bridge_start_320));
1540 sccb_w_array(gspca_dev, ov361x_start_320,
1541 ARRAY_SIZE(ov361x_start_320));
1544 reg_w_array(gspca_dev, ov361x_bridge_start_160,
1545 ARRAY_SIZE(ov361x_bridge_start_160));
1546 sccb_w_array(gspca_dev, ov361x_start_160,
1547 ARRAY_SIZE(ov361x_start_160));
1550 reg_w(gspca_dev, 0xe0, 0x00); /* start transfer */
1552 return gspca_dev->usb_err;
1555 static int sd_start(struct gspca_dev *gspca_dev)
1557 struct sd *sd = (struct sd *) gspca_dev;
1559 if (sd->sensor == SENSOR_OV971x)
1560 return gspca_dev->usb_err;
1561 if (sd->sensor == SENSOR_OV562x)
1562 return gspca_dev->usb_err;
1563 if (sd->sensor == SENSOR_OV361x)
1564 return sd_start_ov361x(gspca_dev);
1566 switch (gspca_dev->curr_mode) {
1567 case QVGA_MODE: /* 320x240 */
1568 sccb_w_array(gspca_dev, ov965x_start_1_vga,
1569 ARRAY_SIZE(ov965x_start_1_vga));
1570 reg_w_array(gspca_dev, bridge_start_qvga,
1571 ARRAY_SIZE(bridge_start_qvga));
1572 sccb_w_array(gspca_dev, ov965x_start_2_qvga,
1573 ARRAY_SIZE(ov965x_start_2_qvga));
1575 case VGA_MODE: /* 640x480 */
1576 sccb_w_array(gspca_dev, ov965x_start_1_vga,
1577 ARRAY_SIZE(ov965x_start_1_vga));
1578 reg_w_array(gspca_dev, bridge_start_vga,
1579 ARRAY_SIZE(bridge_start_vga));
1580 sccb_w_array(gspca_dev, ov965x_start_2_vga,
1581 ARRAY_SIZE(ov965x_start_2_vga));
1583 case SVGA_MODE: /* 800x600 */
1584 sccb_w_array(gspca_dev, ov965x_start_1_svga,
1585 ARRAY_SIZE(ov965x_start_1_svga));
1586 reg_w_array(gspca_dev, bridge_start_svga,
1587 ARRAY_SIZE(bridge_start_svga));
1588 sccb_w_array(gspca_dev, ov965x_start_2_svga,
1589 ARRAY_SIZE(ov965x_start_2_svga));
1591 case XGA_MODE: /* 1024x768 */
1592 sccb_w_array(gspca_dev, ov965x_start_1_xga,
1593 ARRAY_SIZE(ov965x_start_1_xga));
1594 reg_w_array(gspca_dev, bridge_start_xga,
1595 ARRAY_SIZE(bridge_start_xga));
1596 sccb_w_array(gspca_dev, ov965x_start_2_svga,
1597 ARRAY_SIZE(ov965x_start_2_svga));
1600 /* case SXGA_MODE: * 1280x1024 */
1601 sccb_w_array(gspca_dev, ov965x_start_1_sxga,
1602 ARRAY_SIZE(ov965x_start_1_sxga));
1603 reg_w_array(gspca_dev, bridge_start_sxga,
1604 ARRAY_SIZE(bridge_start_sxga));
1605 sccb_w_array(gspca_dev, ov965x_start_2_sxga,
1606 ARRAY_SIZE(ov965x_start_2_sxga));
1610 reg_w(gspca_dev, 0xe0, 0x00);
1611 reg_w(gspca_dev, 0xe0, 0x00);
1612 set_led(gspca_dev, 1);
1613 return gspca_dev->usb_err;
1616 static void sd_stopN(struct gspca_dev *gspca_dev)
1618 if (((struct sd *)gspca_dev)->sensor == SENSOR_OV361x) {
1619 reg_w(gspca_dev, 0xe0, 0x01); /* stop transfer */
1620 /* reg_w(gspca_dev, 0x31, 0x09); */
1623 reg_w(gspca_dev, 0xe0, 0x01);
1624 set_led(gspca_dev, 0);
1625 reg_w(gspca_dev, 0xe0, 0x00);
1628 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1629 #define UVC_STREAM_EOH (1 << 7)
1630 #define UVC_STREAM_ERR (1 << 6)
1631 #define UVC_STREAM_STI (1 << 5)
1632 #define UVC_STREAM_RES (1 << 4)
1633 #define UVC_STREAM_SCR (1 << 3)
1634 #define UVC_STREAM_PTS (1 << 2)
1635 #define UVC_STREAM_EOF (1 << 1)
1636 #define UVC_STREAM_FID (1 << 0)
1638 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1641 struct sd *sd = (struct sd *) gspca_dev;
1644 int remaining_len = len;
1647 payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
1649 len = min(remaining_len, payload_len);
1651 /* Payloads are prefixed with a UVC-style header. We
1652 consider a frame to start when the FID toggles, or the PTS
1653 changes. A frame ends when EOF is set, and we've received
1654 the correct number of bytes. */
1656 /* Verify UVC header. Header length is always 12 */
1657 if (data[0] != 12 || len < 12) {
1658 PDEBUG(D_PACK, "bad header");
1663 if (data[1] & UVC_STREAM_ERR) {
1664 PDEBUG(D_PACK, "payload error");
1668 /* Extract PTS and FID */
1669 if (!(data[1] & UVC_STREAM_PTS)) {
1670 PDEBUG(D_PACK, "PTS not present");
1673 this_pts = (data[5] << 24) | (data[4] << 16)
1674 | (data[3] << 8) | data[2];
1675 this_fid = data[1] & UVC_STREAM_FID;
1677 /* If PTS or FID has changed, start a new frame. */
1678 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
1679 if (gspca_dev->last_packet_type == INTER_PACKET)
1680 gspca_frame_add(gspca_dev, LAST_PACKET,
1682 sd->last_pts = this_pts;
1683 sd->last_fid = this_fid;
1684 gspca_frame_add(gspca_dev, FIRST_PACKET,
1685 data + 12, len - 12);
1686 /* If this packet is marked as EOF, end the frame */
1687 } else if (data[1] & UVC_STREAM_EOF) {
1689 gspca_frame_add(gspca_dev, LAST_PACKET,
1690 data + 12, len - 12);
1693 /* Add the data from this payload */
1694 gspca_frame_add(gspca_dev, INTER_PACKET,
1695 data + 12, len - 12);
1698 /* Done this payload */
1702 /* Discard data until a new frame starts. */
1703 gspca_dev->last_packet_type = DISCARD_PACKET;
1706 remaining_len -= len;
1708 } while (remaining_len > 0);
1711 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
1713 struct gspca_dev *gspca_dev =
1714 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
1716 gspca_dev->usb_err = 0;
1718 if (!gspca_dev->streaming)
1722 case V4L2_CID_BRIGHTNESS:
1723 setbrightness(gspca_dev, ctrl->val);
1725 case V4L2_CID_CONTRAST:
1726 setcontrast(gspca_dev, ctrl->val);
1728 case V4L2_CID_SATURATION:
1729 setsatur(gspca_dev, ctrl->val);
1731 case V4L2_CID_POWER_LINE_FREQUENCY:
1732 setlightfreq(gspca_dev, ctrl->val);
1734 case V4L2_CID_SHARPNESS:
1735 setsharpness(gspca_dev, ctrl->val);
1737 case V4L2_CID_AUTOGAIN:
1739 setautogain(gspca_dev, ctrl->val);
1740 if (!ctrl->val && gspca_dev->exposure->is_new)
1741 setexposure(gspca_dev, gspca_dev->exposure->val);
1744 return gspca_dev->usb_err;
1747 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1748 .s_ctrl = sd_s_ctrl,
1751 static int sd_init_controls(struct gspca_dev *gspca_dev)
1753 struct sd *sd = (struct sd *)gspca_dev;
1754 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1756 if (sd->sensor == SENSOR_OV971x)
1758 if (sd->sensor == SENSOR_OV361x)
1760 gspca_dev->vdev.ctrl_handler = hdl;
1761 v4l2_ctrl_handler_init(hdl, 7);
1762 if (sd->sensor == SENSOR_OV562x) {
1763 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1764 V4L2_CID_BRIGHTNESS, -90, 90, 1, 0);
1766 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1767 V4L2_CID_BRIGHTNESS, 0, 15, 1, 7);
1768 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1769 V4L2_CID_CONTRAST, 0, 15, 1, 3);
1770 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1771 V4L2_CID_SATURATION, 0, 4, 1, 2);
1773 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1774 V4L2_CID_SHARPNESS, -1, 4, 1, -1);
1775 gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1776 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1777 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1778 V4L2_CID_EXPOSURE, 0, 3, 1, 0);
1779 v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
1780 V4L2_CID_POWER_LINE_FREQUENCY,
1781 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0, 0);
1782 v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
1786 pr_err("Could not initialize controls\n");
1792 /* sub-driver description */
1793 static const struct sd_desc sd_desc = {
1794 .name = MODULE_NAME,
1795 .config = sd_config,
1797 .init_controls = sd_init_controls,
1800 .pkt_scan = sd_pkt_scan,
1803 /* -- module initialisation -- */
1804 static const struct usb_device_id device_table[] = {
1805 {USB_DEVICE(0x05a9, 0x8065)},
1806 {USB_DEVICE(0x06f8, 0x3003)},
1807 {USB_DEVICE(0x05a9, 0x1550)},
1811 MODULE_DEVICE_TABLE(usb, device_table);
1813 /* -- device connect -- */
1814 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1816 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1820 static struct usb_driver sd_driver = {
1821 .name = MODULE_NAME,
1822 .id_table = device_table,
1824 .disconnect = gspca_disconnect,
1826 .suspend = gspca_suspend,
1827 .resume = gspca_resume,
1828 .reset_resume = gspca_resume,
1832 module_usb_driver(sd_driver);