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.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #define MODULE_NAME "spca505"
24 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
25 MODULE_DESCRIPTION("GSPCA/SPCA505 USB Camera Driver");
26 MODULE_LICENSE("GPL");
28 /* specific webcam descriptor */
30 struct gspca_dev gspca_dev
; /* !! must be the first item */
33 #define IntelPCCameraPro 0
37 static const struct v4l2_pix_format vga_mode
[] = {
38 {160, 120, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
40 .sizeimage
= 160 * 120 * 3 / 2,
41 .colorspace
= V4L2_COLORSPACE_SRGB
,
43 {176, 144, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
45 .sizeimage
= 176 * 144 * 3 / 2,
46 .colorspace
= V4L2_COLORSPACE_SRGB
,
48 {320, 240, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
50 .sizeimage
= 320 * 240 * 3 / 2,
51 .colorspace
= V4L2_COLORSPACE_SRGB
,
53 {352, 288, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
55 .sizeimage
= 352 * 288 * 3 / 2,
56 .colorspace
= V4L2_COLORSPACE_SRGB
,
58 {640, 480, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
60 .sizeimage
= 640 * 480 * 3 / 2,
61 .colorspace
= V4L2_COLORSPACE_SRGB
,
65 #define SPCA50X_OFFSET_DATA 10
67 #define SPCA50X_REG_USB 0x02 /* spca505 501 */
69 #define SPCA50X_USB_CTRL 0x00 /* spca505 */
70 #define SPCA50X_CUSB_ENABLE 0x01 /* spca505 */
72 #define SPCA50X_REG_GLOBAL 0x03 /* spca505 */
73 #define SPCA50X_GMISC0_IDSEL 0x01 /* Global control device ID select spca505 */
74 #define SPCA50X_GLOBAL_MISC0 0x00 /* Global control miscellaneous 0 spca505 */
76 #define SPCA50X_GLOBAL_MISC1 0x01 /* 505 */
77 #define SPCA50X_GLOBAL_MISC3 0x03 /* 505 */
78 #define SPCA50X_GMISC3_SAA7113RST 0x20 /* Not sure about this one spca505 */
80 /* Image format and compression control */
81 #define SPCA50X_REG_COMPRESS 0x04
84 * Data to initialize a SPCA505. Common to the CCD and external modes
86 static const u8 spca505_init_data
[][3] = {
87 /* bmRequest,value,index */
88 {SPCA50X_REG_GLOBAL
, SPCA50X_GMISC3_SAA7113RST
, SPCA50X_GLOBAL_MISC3
},
90 {SPCA50X_REG_GLOBAL
, 0x00, SPCA50X_GLOBAL_MISC3
},
91 {SPCA50X_REG_GLOBAL
, 0x00, SPCA50X_GLOBAL_MISC1
},
93 {SPCA50X_REG_GLOBAL
, SPCA50X_GMISC0_IDSEL
, SPCA50X_GLOBAL_MISC0
},
96 /* Maybe power down some stuff */
99 /* Setup internal CCD ? */
121 * Data to initialize the camera using the internal CCD
123 static const u8 spca505_open_data_ccd
[][3] = {
124 /* bmRequest,value,index */
125 /* Internal CCD data set */
127 /* This could be a reset */
130 /* Setup compression and image registers. 0x6 and 0x7 seem to be
131 related to H&V hold, and are resolution mode specific */
133 /* DIFF(0x50), was (0x10) */
140 /* DIFF (0x4a), was (0xa) */
145 /* DIFF not written */
147 /* DIFF not written */
149 /* DIFF not written */
151 /* DIFF not written */
153 /* DIFF not written */
155 /* DIFF not written */
157 /* DIFF not written */
159 /* DIFF not written */
161 /* DIFF not written */
163 /* DIFF not written */
165 /* DIFF not written */
167 /* DIFF not written */
169 /* DIFF not written */
172 /* DIFF (0x3,0x28,0x3) */
175 /* DIFF not written */
179 /* Extra block copied from init to hopefully ensure CCD is in a sane state */
197 /* End of extra block */
264 /* Compress = OFF (0x1 to turn on) */
270 /* was 5 (Image Type ? ) */
302 * Made by Tomasz Zablocki (skalamandra@poczta.onet.pl)
303 * SPCA505b chip based cameras initialization data
306 #define initial_brightness 0x7f /* 0x0(white)-0xff(black) */
307 /* #define initial_brightness 0x0 //0x0(white)-0xff(black) */
309 * Data to initialize a SPCA505. Common to the CCD and external modes
311 static const u8 spca505b_init_data
[][3] = {
313 {0x02, 0x00, 0x00}, /* init */
369 * Data to initialize the camera using the internal CCD
371 static const u8 spca505b_open_data_ccd
[][3] = {
373 /* {0x02,0x00,0x00}, */
374 {0x03, 0x04, 0x01}, /* rst */
400 {0x05, initial_brightness
>> 6, 0x00},
401 {0x05, (initial_brightness
<< 2) & 0xff, 0x01},
411 {0x05, 0x80, 0x14}, /* max exposure off (0=on) */
430 {0x06, 0x11, 0x10}, /* contrast */
495 {0x06, 0x4d, 0x51}, /* maybe saturation (4d) */
496 {0x06, 0x84, 0x53}, /* making green (84) */
497 {0x06, 0x00, 0x57}, /* sharpness (1) */
502 {0x06, 0x18, 0x0c}, /* maybe hue (18) */
506 {0x06, 0x18, 0x10}, /* maybe contrast (18) */
510 {0x04, 0x00, 0x08}, /* compression */
515 {0x04, 0x1d, 0x00}, /* imagetype (1d) */
516 {0x04, 0x41, 0x01}, /* hardware snapcontrol */
534 {0x05, initial_brightness
>> 6, 0x00},
535 {0x05, (initial_brightness
<< 2) & 0xff, 0x01},
543 static int reg_write(struct gspca_dev
*gspca_dev
,
544 u16 req
, u16 index
, u16 value
)
547 struct usb_device
*dev
= gspca_dev
->dev
;
549 ret
= usb_control_msg(dev
,
550 usb_sndctrlpipe(dev
, 0),
552 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
553 value
, index
, NULL
, 0, 500);
554 gspca_dbg(gspca_dev
, D_USBO
, "reg write: 0x%02x,0x%02x:0x%02x, %d\n",
555 req
, index
, value
, ret
);
557 pr_err("reg write: error %d\n", ret
);
561 /* returns: negative is error, pos or zero is data */
562 static int reg_read(struct gspca_dev
*gspca_dev
,
563 u16 req
, /* bRequest */
564 u16 index
) /* wIndex */
568 ret
= usb_control_msg(gspca_dev
->dev
,
569 usb_rcvctrlpipe(gspca_dev
->dev
, 0),
571 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
574 gspca_dev
->usb_buf
, 2,
578 return (gspca_dev
->usb_buf
[1] << 8) + gspca_dev
->usb_buf
[0];
581 static int write_vector(struct gspca_dev
*gspca_dev
,
586 while (data
[i
][0] != 0) {
587 ret
= reg_write(gspca_dev
, data
[i
][0], data
[i
][2],
596 /* this function is called at probe time */
597 static int sd_config(struct gspca_dev
*gspca_dev
,
598 const struct usb_device_id
*id
)
600 struct sd
*sd
= (struct sd
*) gspca_dev
;
603 cam
= &gspca_dev
->cam
;
604 cam
->cam_mode
= vga_mode
;
605 sd
->subtype
= id
->driver_info
;
606 if (sd
->subtype
!= IntelPCCameraPro
)
607 cam
->nmodes
= ARRAY_SIZE(vga_mode
);
608 else /* no 640x480 for IntelPCCameraPro */
609 cam
->nmodes
= ARRAY_SIZE(vga_mode
) - 1;
614 /* this function is called at probe and resume time */
615 static int sd_init(struct gspca_dev
*gspca_dev
)
617 struct sd
*sd
= (struct sd
*) gspca_dev
;
619 if (write_vector(gspca_dev
,
620 sd
->subtype
== Nxultra
622 : spca505_init_data
))
627 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 brightness
)
629 reg_write(gspca_dev
, 0x05, 0x00, (255 - brightness
) >> 6);
630 reg_write(gspca_dev
, 0x05, 0x01, (255 - brightness
) << 2);
633 static int sd_start(struct gspca_dev
*gspca_dev
)
635 struct sd
*sd
= (struct sd
*) gspca_dev
;
637 static u8 mode_tb
[][3] = {
639 {0x00, 0x10, 0x10}, /* 640x480 */
640 {0x01, 0x1a, 0x1a}, /* 352x288 */
641 {0x02, 0x1c, 0x1d}, /* 320x240 */
642 {0x04, 0x34, 0x34}, /* 176x144 */
643 {0x05, 0x40, 0x40} /* 160x120 */
646 if (sd
->subtype
== Nxultra
)
647 write_vector(gspca_dev
, spca505b_open_data_ccd
);
649 write_vector(gspca_dev
, spca505_open_data_ccd
);
650 ret
= reg_read(gspca_dev
, 0x06, 0x16);
653 gspca_err(gspca_dev
, "register read failed err: %d\n", ret
);
657 pr_err("After vector read returns 0x%04x should be 0x0101\n",
661 ret
= reg_write(gspca_dev
, 0x06, 0x16, 0x0a);
664 reg_write(gspca_dev
, 0x05, 0xc2, 0x12);
666 /* necessary because without it we can see stream
667 * only once after loading module */
668 /* stopping usb registers Tomasz change */
669 reg_write(gspca_dev
, 0x02, 0x00, 0x00);
671 mode
= gspca_dev
->cam
.cam_mode
[(int) gspca_dev
->curr_mode
].priv
;
672 reg_write(gspca_dev
, SPCA50X_REG_COMPRESS
, 0x00, mode_tb
[mode
][0]);
673 reg_write(gspca_dev
, SPCA50X_REG_COMPRESS
, 0x06, mode_tb
[mode
][1]);
674 reg_write(gspca_dev
, SPCA50X_REG_COMPRESS
, 0x07, mode_tb
[mode
][2]);
676 return reg_write(gspca_dev
, SPCA50X_REG_USB
,
678 SPCA50X_CUSB_ENABLE
);
681 static void sd_stopN(struct gspca_dev
*gspca_dev
)
683 /* Disable ISO packet machine */
684 reg_write(gspca_dev
, 0x02, 0x00, 0x00);
687 /* called on streamoff with alt 0 and on disconnect */
688 static void sd_stop0(struct gspca_dev
*gspca_dev
)
690 if (!gspca_dev
->present
)
693 /* This maybe reset or power control */
694 reg_write(gspca_dev
, 0x03, 0x03, 0x20);
695 reg_write(gspca_dev
, 0x03, 0x01, 0x00);
696 reg_write(gspca_dev
, 0x03, 0x00, 0x01);
697 reg_write(gspca_dev
, 0x05, 0x10, 0x01);
698 reg_write(gspca_dev
, 0x05, 0x11, 0x0f);
701 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
702 u8
*data
, /* isoc packet */
703 int len
) /* iso packet length */
706 case 0: /* start of frame */
707 gspca_frame_add(gspca_dev
, LAST_PACKET
, NULL
, 0);
708 data
+= SPCA50X_OFFSET_DATA
;
709 len
-= SPCA50X_OFFSET_DATA
;
710 gspca_frame_add(gspca_dev
, FIRST_PACKET
, data
, len
);
712 case 0xff: /* drop */
717 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
722 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
724 struct gspca_dev
*gspca_dev
=
725 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
727 gspca_dev
->usb_err
= 0;
729 if (!gspca_dev
->streaming
)
733 case V4L2_CID_BRIGHTNESS
:
734 setbrightness(gspca_dev
, ctrl
->val
);
737 return gspca_dev
->usb_err
;
740 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
744 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
746 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
748 gspca_dev
->vdev
.ctrl_handler
= hdl
;
749 v4l2_ctrl_handler_init(hdl
, 5);
750 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
751 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 127);
754 pr_err("Could not initialize controls\n");
760 /* sub-driver description */
761 static const struct sd_desc sd_desc
= {
764 .init_controls
= sd_init_controls
,
769 .pkt_scan
= sd_pkt_scan
,
772 /* -- module initialisation -- */
773 static const struct usb_device_id device_table
[] = {
774 {USB_DEVICE(0x041e, 0x401d), .driver_info
= Nxultra
},
775 {USB_DEVICE(0x0733, 0x0430), .driver_info
= IntelPCCameraPro
},
776 /*fixme: may be UsbGrabberPV321 BRIDGE_SPCA506 SENSOR_SAA7113 */
779 MODULE_DEVICE_TABLE(usb
, device_table
);
781 /* -- device connect -- */
782 static int sd_probe(struct usb_interface
*intf
,
783 const struct usb_device_id
*id
)
785 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
789 static struct usb_driver sd_driver
= {
791 .id_table
= device_table
,
793 .disconnect
= gspca_disconnect
,
795 .suspend
= gspca_suspend
,
796 .resume
= gspca_resume
,
797 .reset_resume
= gspca_resume
,
801 module_usb_driver(sd_driver
);