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 MODULE_NAME "spca505"
26 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
27 MODULE_DESCRIPTION("GSPCA/SPCA505 USB Camera Driver");
28 MODULE_LICENSE("GPL");
30 /* specific webcam descriptor */
32 struct gspca_dev gspca_dev; /* !! must be the first item */
37 #define IntelPCCameraPro 0
41 /* V4L2 controls supported by the driver */
42 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
43 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
45 static struct ctrl sd_ctrls[] = {
48 .id = V4L2_CID_BRIGHTNESS,
49 .type = V4L2_CTRL_TYPE_INTEGER,
54 #define BRIGHTNESS_DEF 127
55 .default_value = BRIGHTNESS_DEF,
57 .set = sd_setbrightness,
58 .get = sd_getbrightness,
62 static const struct v4l2_pix_format vga_mode[] = {
63 {160, 120, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE,
65 .sizeimage = 160 * 120 * 3 / 2,
66 .colorspace = V4L2_COLORSPACE_SRGB,
68 {176, 144, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE,
70 .sizeimage = 176 * 144 * 3 / 2,
71 .colorspace = V4L2_COLORSPACE_SRGB,
73 {320, 240, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE,
75 .sizeimage = 320 * 240 * 3 / 2,
76 .colorspace = V4L2_COLORSPACE_SRGB,
78 {352, 288, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE,
80 .sizeimage = 352 * 288 * 3 / 2,
81 .colorspace = V4L2_COLORSPACE_SRGB,
83 {640, 480, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE,
85 .sizeimage = 640 * 480 * 3 / 2,
86 .colorspace = V4L2_COLORSPACE_SRGB,
90 #define SPCA50X_OFFSET_DATA 10
92 #define SPCA50X_REG_USB 0x02 /* spca505 501 */
94 #define SPCA50X_USB_CTRL 0x00 /* spca505 */
95 #define SPCA50X_CUSB_ENABLE 0x01 /* spca505 */
97 #define SPCA50X_REG_GLOBAL 0x03 /* spca505 */
98 #define SPCA50X_GMISC0_IDSEL 0x01 /* Global control device ID select spca505 */
99 #define SPCA50X_GLOBAL_MISC0 0x00 /* Global control miscellaneous 0 spca505 */
101 #define SPCA50X_GLOBAL_MISC1 0x01 /* 505 */
102 #define SPCA50X_GLOBAL_MISC3 0x03 /* 505 */
103 #define SPCA50X_GMISC3_SAA7113RST 0x20 /* Not sure about this one spca505 */
105 /* Image format and compression control */
106 #define SPCA50X_REG_COMPRESS 0x04
109 * Data to initialize a SPCA505. Common to the CCD and external modes
111 static const u8 spca505_init_data[][3] = {
112 /* bmRequest,value,index */
113 {SPCA50X_REG_GLOBAL, SPCA50X_GMISC3_SAA7113RST, SPCA50X_GLOBAL_MISC3},
115 {SPCA50X_REG_GLOBAL, 0x00, SPCA50X_GLOBAL_MISC3},
116 {SPCA50X_REG_GLOBAL, 0x00, SPCA50X_GLOBAL_MISC1},
117 /* Block USB reset */
118 {SPCA50X_REG_GLOBAL, SPCA50X_GMISC0_IDSEL, SPCA50X_GLOBAL_MISC0},
121 /* Maybe power down some stuff */
124 /* Setup internal CCD ? */
146 * Data to initialize the camera using the internal CCD
148 static const u8 spca505_open_data_ccd[][3] = {
149 /* bmRequest,value,index */
150 /* Internal CCD data set */
152 /* This could be a reset */
155 /* Setup compression and image registers. 0x6 and 0x7 seem to be
156 related to H&V hold, and are resolution mode specific */
158 /* DIFF(0x50), was (0x10) */
165 /* DIFF (0x4a), was (0xa) */
170 /* DIFF not written */
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 */
197 /* DIFF (0x3,0x28,0x3) */
200 /* DIFF not written */
204 /* Extra block copied from init to hopefully ensure CCD is in a sane state */
222 /* End of extra block */
289 /* Compress = OFF (0x1 to turn on) */
295 /* was 5 (Image Type ? ) */
327 * Made by Tomasz Zablocki (skalamandra@poczta.onet.pl)
328 * SPCA505b chip based cameras initialization data
331 #define initial_brightness 0x7f /* 0x0(white)-0xff(black) */
332 /* #define initial_brightness 0x0 //0x0(white)-0xff(black) */
334 * Data to initialize a SPCA505. Common to the CCD and external modes
336 static const u8 spca505b_init_data[][3] = {
338 {0x02, 0x00, 0x00}, /* init */
398 * Data to initialize the camera using the internal CCD
400 static const u8 spca505b_open_data_ccd[][3] = {
402 /* {0x02,0x00,0x00}, */
403 {0x03, 0x04, 0x01}, /* rst */
429 {0x05, initial_brightness >> 6, 0x00},
430 {0x05, (initial_brightness << 2) & 0xff, 0x01},
440 {0x05, 0x80, 0x14}, /* max exposure off (0=on) */
459 {0x06, 0x11, 0x10}, /* contrast */
524 {0x06, 0x4d, 0x51}, /* maybe saturation (4d) */
525 {0x06, 0x84, 0x53}, /* making green (84) */
526 {0x06, 0x00, 0x57}, /* sharpness (1) */
531 {0x06, 0x18, 0x0c}, /* maybe hue (18) */
535 {0x06, 0x18, 0x10}, /* maybe contrast (18) */
539 {0x04, 0x00, 0x08}, /* compression */
544 {0x04, 0x1d, 0x00}, /* imagetype (1d) */
545 {0x04, 0x41, 0x01}, /* hardware snapcontrol */
563 {0x05, initial_brightness >> 6, 0x00},
564 {0x05, (initial_brightness << 2) & 0xff, 0x01},
572 static int reg_write(struct usb_device *dev,
573 u16 req, u16 index, u16 value)
577 ret = usb_control_msg(dev,
578 usb_sndctrlpipe(dev, 0),
580 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
581 value, index, NULL, 0, 500);
582 PDEBUG(D_USBO, "reg write: 0x%02x,0x%02x:0x%02x, %d",
583 req, index, value, ret);
585 PDEBUG(D_ERR, "reg write: error %d", ret);
589 /* returns: negative is error, pos or zero is data */
590 static int reg_read(struct gspca_dev *gspca_dev,
591 u16 req, /* bRequest */
592 u16 index) /* wIndex */
596 ret = usb_control_msg(gspca_dev->dev,
597 usb_rcvctrlpipe(gspca_dev->dev, 0),
599 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
602 gspca_dev->usb_buf, 2,
606 return (gspca_dev->usb_buf[1] << 8) + gspca_dev->usb_buf[0];
609 static int write_vector(struct gspca_dev *gspca_dev,
612 struct usb_device *dev = gspca_dev->dev;
615 while (data[i][0] != 0) {
616 ret = reg_write(dev, data[i][0], data[i][2], data[i][1]);
624 /* this function is called at probe time */
625 static int sd_config(struct gspca_dev *gspca_dev,
626 const struct usb_device_id *id)
628 struct sd *sd = (struct sd *) gspca_dev;
631 cam = &gspca_dev->cam;
632 cam->cam_mode = vga_mode;
633 sd->subtype = id->driver_info;
634 if (sd->subtype != IntelPCCameraPro)
635 cam->nmodes = ARRAY_SIZE(vga_mode);
636 else /* no 640x480 for IntelPCCameraPro */
637 cam->nmodes = ARRAY_SIZE(vga_mode) - 1;
638 sd->brightness = BRIGHTNESS_DEF;
643 /* this function is called at probe and resume time */
644 static int sd_init(struct gspca_dev *gspca_dev)
646 struct sd *sd = (struct sd *) gspca_dev;
648 if (write_vector(gspca_dev,
649 sd->subtype == Nxultra
651 : spca505_init_data))
656 static void setbrightness(struct gspca_dev *gspca_dev)
658 struct sd *sd = (struct sd *) gspca_dev;
659 u8 brightness = sd->brightness;
661 reg_write(gspca_dev->dev, 0x05, 0x00, (255 - brightness) >> 6);
662 reg_write(gspca_dev->dev, 0x05, 0x01, (255 - brightness) << 2);
665 static int sd_start(struct gspca_dev *gspca_dev)
667 struct sd *sd = (struct sd *) gspca_dev;
668 struct usb_device *dev = gspca_dev->dev;
670 static u8 mode_tb[][3] = {
672 {0x00, 0x10, 0x10}, /* 640x480 */
673 {0x01, 0x1a, 0x1a}, /* 352x288 */
674 {0x02, 0x1c, 0x1d}, /* 320x240 */
675 {0x04, 0x34, 0x34}, /* 176x144 */
676 {0x05, 0x40, 0x40} /* 160x120 */
679 if (sd->subtype == Nxultra)
680 write_vector(gspca_dev, spca505b_open_data_ccd);
682 write_vector(gspca_dev, spca505_open_data_ccd);
683 ret = reg_read(gspca_dev, 0x06, 0x16);
687 "register read failed err: %d",
693 "After vector read returns 0x%04x should be 0x0101",
697 ret = reg_write(gspca_dev->dev, 0x06, 0x16, 0x0a);
700 reg_write(gspca_dev->dev, 0x05, 0xc2, 0x12);
702 /* necessary because without it we can see stream
703 * only once after loading module */
704 /* stopping usb registers Tomasz change */
705 reg_write(dev, 0x02, 0x00, 0x00);
707 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
708 reg_write(dev, SPCA50X_REG_COMPRESS, 0x00, mode_tb[mode][0]);
709 reg_write(dev, SPCA50X_REG_COMPRESS, 0x06, mode_tb[mode][1]);
710 reg_write(dev, SPCA50X_REG_COMPRESS, 0x07, mode_tb[mode][2]);
712 ret = reg_write(dev, SPCA50X_REG_USB,
714 SPCA50X_CUSB_ENABLE);
716 setbrightness(gspca_dev);
721 static void sd_stopN(struct gspca_dev *gspca_dev)
723 /* Disable ISO packet machine */
724 reg_write(gspca_dev->dev, 0x02, 0x00, 0x00);
727 /* called on streamoff with alt 0 and on disconnect */
728 static void sd_stop0(struct gspca_dev *gspca_dev)
730 if (!gspca_dev->present)
733 /* This maybe reset or power control */
734 reg_write(gspca_dev->dev, 0x03, 0x03, 0x20);
735 reg_write(gspca_dev->dev, 0x03, 0x01, 0x00);
736 reg_write(gspca_dev->dev, 0x03, 0x00, 0x01);
737 reg_write(gspca_dev->dev, 0x05, 0x10, 0x01);
738 reg_write(gspca_dev->dev, 0x05, 0x11, 0x0f);
741 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
742 struct gspca_frame *frame, /* target */
743 u8 *data, /* isoc packet */
744 int len) /* iso packet length */
747 case 0: /* start of frame */
748 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
750 data += SPCA50X_OFFSET_DATA;
751 len -= SPCA50X_OFFSET_DATA;
752 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
755 case 0xff: /* drop */
760 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
766 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
768 struct sd *sd = (struct sd *) gspca_dev;
770 sd->brightness = val;
771 if (gspca_dev->streaming)
772 setbrightness(gspca_dev);
776 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
778 struct sd *sd = (struct sd *) gspca_dev;
780 *val = sd->brightness;
784 /* sub-driver description */
785 static const struct sd_desc sd_desc = {
788 .nctrls = ARRAY_SIZE(sd_ctrls),
794 .pkt_scan = sd_pkt_scan,
797 /* -- module initialisation -- */
798 static const __devinitdata struct usb_device_id device_table[] = {
799 {USB_DEVICE(0x041e, 0x401d), .driver_info = Nxultra},
800 {USB_DEVICE(0x0733, 0x0430), .driver_info = IntelPCCameraPro},
801 /*fixme: may be UsbGrabberPV321 BRIDGE_SPCA506 SENSOR_SAA7113 */
804 MODULE_DEVICE_TABLE(usb, device_table);
806 /* -- device connect -- */
807 static int sd_probe(struct usb_interface *intf,
808 const struct usb_device_id *id)
810 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
814 static struct usb_driver sd_driver = {
816 .id_table = device_table,
818 .disconnect = gspca_disconnect,
820 .suspend = gspca_suspend,
821 .resume = gspca_resume,
825 /* -- module insert / remove -- */
826 static int __init sd_mod_init(void)
830 ret = usb_register(&sd_driver);
833 PDEBUG(D_PROBE, "registered");
836 static void __exit sd_mod_exit(void)
838 usb_deregister(&sd_driver);
839 PDEBUG(D_PROBE, "deregistered");
842 module_init(sd_mod_init);
843 module_exit(sd_mod_exit);