2 * SPCA505 chip based cameras initialization data
4 * V4L2 by Jean-Francis Moine <http://moinejf.free.fr>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #define MODULE_NAME "spca505"
28 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
29 MODULE_DESCRIPTION("GSPCA/SPCA505 USB Camera Driver");
30 MODULE_LICENSE("GPL");
32 /* specific webcam descriptor */
34 struct gspca_dev gspca_dev
; /* !! must be the first item */
39 #define IntelPCCameraPro 0
43 /* V4L2 controls supported by the driver */
44 static int sd_setbrightness(struct gspca_dev
*gspca_dev
, __s32 val
);
45 static int sd_getbrightness(struct gspca_dev
*gspca_dev
, __s32
*val
);
47 static const struct ctrl sd_ctrls
[] = {
50 .id
= V4L2_CID_BRIGHTNESS
,
51 .type
= V4L2_CTRL_TYPE_INTEGER
,
56 #define BRIGHTNESS_DEF 127
57 .default_value
= BRIGHTNESS_DEF
,
59 .set
= sd_setbrightness
,
60 .get
= sd_getbrightness
,
64 static const struct v4l2_pix_format vga_mode
[] = {
65 {160, 120, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
67 .sizeimage
= 160 * 120 * 3 / 2,
68 .colorspace
= V4L2_COLORSPACE_SRGB
,
70 {176, 144, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
72 .sizeimage
= 176 * 144 * 3 / 2,
73 .colorspace
= V4L2_COLORSPACE_SRGB
,
75 {320, 240, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
77 .sizeimage
= 320 * 240 * 3 / 2,
78 .colorspace
= V4L2_COLORSPACE_SRGB
,
80 {352, 288, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
82 .sizeimage
= 352 * 288 * 3 / 2,
83 .colorspace
= V4L2_COLORSPACE_SRGB
,
85 {640, 480, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
87 .sizeimage
= 640 * 480 * 3 / 2,
88 .colorspace
= V4L2_COLORSPACE_SRGB
,
92 #define SPCA50X_OFFSET_DATA 10
94 #define SPCA50X_REG_USB 0x02 /* spca505 501 */
96 #define SPCA50X_USB_CTRL 0x00 /* spca505 */
97 #define SPCA50X_CUSB_ENABLE 0x01 /* spca505 */
99 #define SPCA50X_REG_GLOBAL 0x03 /* spca505 */
100 #define SPCA50X_GMISC0_IDSEL 0x01 /* Global control device ID select spca505 */
101 #define SPCA50X_GLOBAL_MISC0 0x00 /* Global control miscellaneous 0 spca505 */
103 #define SPCA50X_GLOBAL_MISC1 0x01 /* 505 */
104 #define SPCA50X_GLOBAL_MISC3 0x03 /* 505 */
105 #define SPCA50X_GMISC3_SAA7113RST 0x20 /* Not sure about this one spca505 */
107 /* Image format and compression control */
108 #define SPCA50X_REG_COMPRESS 0x04
111 * Data to initialize a SPCA505. Common to the CCD and external modes
113 static const u8 spca505_init_data
[][3] = {
114 /* bmRequest,value,index */
115 {SPCA50X_REG_GLOBAL
, SPCA50X_GMISC3_SAA7113RST
, SPCA50X_GLOBAL_MISC3
},
117 {SPCA50X_REG_GLOBAL
, 0x00, SPCA50X_GLOBAL_MISC3
},
118 {SPCA50X_REG_GLOBAL
, 0x00, SPCA50X_GLOBAL_MISC1
},
119 /* Block USB reset */
120 {SPCA50X_REG_GLOBAL
, SPCA50X_GMISC0_IDSEL
, SPCA50X_GLOBAL_MISC0
},
123 /* Maybe power down some stuff */
126 /* Setup internal CCD ? */
148 * Data to initialize the camera using the internal CCD
150 static const u8 spca505_open_data_ccd
[][3] = {
151 /* bmRequest,value,index */
152 /* Internal CCD data set */
154 /* This could be a reset */
157 /* Setup compression and image registers. 0x6 and 0x7 seem to be
158 related to H&V hold, and are resolution mode specific */
160 /* DIFF(0x50), was (0x10) */
167 /* DIFF (0x4a), was (0xa) */
172 /* DIFF not written */
174 /* DIFF not written */
176 /* DIFF not written */
178 /* DIFF not written */
180 /* DIFF not written */
182 /* DIFF not written */
184 /* DIFF not written */
186 /* DIFF not written */
188 /* DIFF not written */
190 /* DIFF not written */
192 /* DIFF not written */
194 /* DIFF not written */
196 /* DIFF not written */
199 /* DIFF (0x3,0x28,0x3) */
202 /* DIFF not written */
206 /* Extra block copied from init to hopefully ensure CCD is in a sane state */
224 /* End of extra block */
291 /* Compress = OFF (0x1 to turn on) */
297 /* was 5 (Image Type ? ) */
329 * Made by Tomasz Zablocki (skalamandra@poczta.onet.pl)
330 * SPCA505b chip based cameras initialization data
333 #define initial_brightness 0x7f /* 0x0(white)-0xff(black) */
334 /* #define initial_brightness 0x0 //0x0(white)-0xff(black) */
336 * Data to initialize a SPCA505. Common to the CCD and external modes
338 static const u8 spca505b_init_data
[][3] = {
340 {0x02, 0x00, 0x00}, /* init */
396 * Data to initialize the camera using the internal CCD
398 static const u8 spca505b_open_data_ccd
[][3] = {
400 /* {0x02,0x00,0x00}, */
401 {0x03, 0x04, 0x01}, /* rst */
427 {0x05, initial_brightness
>> 6, 0x00},
428 {0x05, (initial_brightness
<< 2) & 0xff, 0x01},
438 {0x05, 0x80, 0x14}, /* max exposure off (0=on) */
457 {0x06, 0x11, 0x10}, /* contrast */
522 {0x06, 0x4d, 0x51}, /* maybe saturation (4d) */
523 {0x06, 0x84, 0x53}, /* making green (84) */
524 {0x06, 0x00, 0x57}, /* sharpness (1) */
529 {0x06, 0x18, 0x0c}, /* maybe hue (18) */
533 {0x06, 0x18, 0x10}, /* maybe contrast (18) */
537 {0x04, 0x00, 0x08}, /* compression */
542 {0x04, 0x1d, 0x00}, /* imagetype (1d) */
543 {0x04, 0x41, 0x01}, /* hardware snapcontrol */
561 {0x05, initial_brightness
>> 6, 0x00},
562 {0x05, (initial_brightness
<< 2) & 0xff, 0x01},
570 static int reg_write(struct usb_device
*dev
,
571 u16 req
, u16 index
, u16 value
)
575 ret
= usb_control_msg(dev
,
576 usb_sndctrlpipe(dev
, 0),
578 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
579 value
, index
, NULL
, 0, 500);
580 PDEBUG(D_USBO
, "reg write: 0x%02x,0x%02x:0x%02x, %d",
581 req
, index
, value
, ret
);
583 pr_err("reg write: error %d\n", ret
);
587 /* returns: negative is error, pos or zero is data */
588 static int reg_read(struct gspca_dev
*gspca_dev
,
589 u16 req
, /* bRequest */
590 u16 index
) /* wIndex */
594 ret
= usb_control_msg(gspca_dev
->dev
,
595 usb_rcvctrlpipe(gspca_dev
->dev
, 0),
597 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
600 gspca_dev
->usb_buf
, 2,
604 return (gspca_dev
->usb_buf
[1] << 8) + gspca_dev
->usb_buf
[0];
607 static int write_vector(struct gspca_dev
*gspca_dev
,
610 struct usb_device
*dev
= gspca_dev
->dev
;
613 while (data
[i
][0] != 0) {
614 ret
= reg_write(dev
, data
[i
][0], data
[i
][2], data
[i
][1]);
622 /* this function is called at probe time */
623 static int sd_config(struct gspca_dev
*gspca_dev
,
624 const struct usb_device_id
*id
)
626 struct sd
*sd
= (struct sd
*) gspca_dev
;
629 cam
= &gspca_dev
->cam
;
630 cam
->cam_mode
= vga_mode
;
631 sd
->subtype
= id
->driver_info
;
632 if (sd
->subtype
!= IntelPCCameraPro
)
633 cam
->nmodes
= ARRAY_SIZE(vga_mode
);
634 else /* no 640x480 for IntelPCCameraPro */
635 cam
->nmodes
= ARRAY_SIZE(vga_mode
) - 1;
636 sd
->brightness
= BRIGHTNESS_DEF
;
641 /* this function is called at probe and resume time */
642 static int sd_init(struct gspca_dev
*gspca_dev
)
644 struct sd
*sd
= (struct sd
*) gspca_dev
;
646 if (write_vector(gspca_dev
,
647 sd
->subtype
== Nxultra
649 : spca505_init_data
))
654 static void setbrightness(struct gspca_dev
*gspca_dev
)
656 struct sd
*sd
= (struct sd
*) gspca_dev
;
657 u8 brightness
= sd
->brightness
;
659 reg_write(gspca_dev
->dev
, 0x05, 0x00, (255 - brightness
) >> 6);
660 reg_write(gspca_dev
->dev
, 0x05, 0x01, (255 - brightness
) << 2);
663 static int sd_start(struct gspca_dev
*gspca_dev
)
665 struct sd
*sd
= (struct sd
*) gspca_dev
;
666 struct usb_device
*dev
= gspca_dev
->dev
;
668 static u8 mode_tb
[][3] = {
670 {0x00, 0x10, 0x10}, /* 640x480 */
671 {0x01, 0x1a, 0x1a}, /* 352x288 */
672 {0x02, 0x1c, 0x1d}, /* 320x240 */
673 {0x04, 0x34, 0x34}, /* 176x144 */
674 {0x05, 0x40, 0x40} /* 160x120 */
677 if (sd
->subtype
== Nxultra
)
678 write_vector(gspca_dev
, spca505b_open_data_ccd
);
680 write_vector(gspca_dev
, spca505_open_data_ccd
);
681 ret
= reg_read(gspca_dev
, 0x06, 0x16);
685 "register read failed err: %d",
690 pr_err("After vector read returns 0x%04x should be 0x0101\n",
694 ret
= reg_write(gspca_dev
->dev
, 0x06, 0x16, 0x0a);
697 reg_write(gspca_dev
->dev
, 0x05, 0xc2, 0x12);
699 /* necessary because without it we can see stream
700 * only once after loading module */
701 /* stopping usb registers Tomasz change */
702 reg_write(dev
, 0x02, 0x00, 0x00);
704 mode
= gspca_dev
->cam
.cam_mode
[(int) gspca_dev
->curr_mode
].priv
;
705 reg_write(dev
, SPCA50X_REG_COMPRESS
, 0x00, mode_tb
[mode
][0]);
706 reg_write(dev
, SPCA50X_REG_COMPRESS
, 0x06, mode_tb
[mode
][1]);
707 reg_write(dev
, SPCA50X_REG_COMPRESS
, 0x07, mode_tb
[mode
][2]);
709 ret
= reg_write(dev
, SPCA50X_REG_USB
,
711 SPCA50X_CUSB_ENABLE
);
713 setbrightness(gspca_dev
);
718 static void sd_stopN(struct gspca_dev
*gspca_dev
)
720 /* Disable ISO packet machine */
721 reg_write(gspca_dev
->dev
, 0x02, 0x00, 0x00);
724 /* called on streamoff with alt 0 and on disconnect */
725 static void sd_stop0(struct gspca_dev
*gspca_dev
)
727 if (!gspca_dev
->present
)
730 /* This maybe reset or power control */
731 reg_write(gspca_dev
->dev
, 0x03, 0x03, 0x20);
732 reg_write(gspca_dev
->dev
, 0x03, 0x01, 0x00);
733 reg_write(gspca_dev
->dev
, 0x03, 0x00, 0x01);
734 reg_write(gspca_dev
->dev
, 0x05, 0x10, 0x01);
735 reg_write(gspca_dev
->dev
, 0x05, 0x11, 0x0f);
738 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
739 u8
*data
, /* isoc packet */
740 int len
) /* iso packet length */
743 case 0: /* start of frame */
744 gspca_frame_add(gspca_dev
, LAST_PACKET
, NULL
, 0);
745 data
+= SPCA50X_OFFSET_DATA
;
746 len
-= SPCA50X_OFFSET_DATA
;
747 gspca_frame_add(gspca_dev
, FIRST_PACKET
, data
, len
);
749 case 0xff: /* drop */
754 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
759 static int sd_setbrightness(struct gspca_dev
*gspca_dev
, __s32 val
)
761 struct sd
*sd
= (struct sd
*) gspca_dev
;
763 sd
->brightness
= val
;
764 if (gspca_dev
->streaming
)
765 setbrightness(gspca_dev
);
769 static int sd_getbrightness(struct gspca_dev
*gspca_dev
, __s32
*val
)
771 struct sd
*sd
= (struct sd
*) gspca_dev
;
773 *val
= sd
->brightness
;
777 /* sub-driver description */
778 static const struct sd_desc sd_desc
= {
781 .nctrls
= ARRAY_SIZE(sd_ctrls
),
787 .pkt_scan
= sd_pkt_scan
,
790 /* -- module initialisation -- */
791 static const struct usb_device_id device_table
[] = {
792 {USB_DEVICE(0x041e, 0x401d), .driver_info
= Nxultra
},
793 {USB_DEVICE(0x0733, 0x0430), .driver_info
= IntelPCCameraPro
},
794 /*fixme: may be UsbGrabberPV321 BRIDGE_SPCA506 SENSOR_SAA7113 */
797 MODULE_DEVICE_TABLE(usb
, device_table
);
799 /* -- device connect -- */
800 static int sd_probe(struct usb_interface
*intf
,
801 const struct usb_device_id
*id
)
803 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
807 static struct usb_driver sd_driver
= {
809 .id_table
= device_table
,
811 .disconnect
= gspca_disconnect
,
813 .suspend
= gspca_suspend
,
814 .resume
= gspca_resume
,
818 module_usb_driver(sd_driver
);