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 */
37 #define IntelPCCameraPro 0
41 static const struct v4l2_pix_format vga_mode
[] = {
42 {160, 120, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
44 .sizeimage
= 160 * 120 * 3 / 2,
45 .colorspace
= V4L2_COLORSPACE_SRGB
,
47 {176, 144, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
49 .sizeimage
= 176 * 144 * 3 / 2,
50 .colorspace
= V4L2_COLORSPACE_SRGB
,
52 {320, 240, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
54 .sizeimage
= 320 * 240 * 3 / 2,
55 .colorspace
= V4L2_COLORSPACE_SRGB
,
57 {352, 288, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
59 .sizeimage
= 352 * 288 * 3 / 2,
60 .colorspace
= V4L2_COLORSPACE_SRGB
,
62 {640, 480, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
64 .sizeimage
= 640 * 480 * 3 / 2,
65 .colorspace
= V4L2_COLORSPACE_SRGB
,
69 #define SPCA50X_OFFSET_DATA 10
71 #define SPCA50X_REG_USB 0x02 /* spca505 501 */
73 #define SPCA50X_USB_CTRL 0x00 /* spca505 */
74 #define SPCA50X_CUSB_ENABLE 0x01 /* spca505 */
76 #define SPCA50X_REG_GLOBAL 0x03 /* spca505 */
77 #define SPCA50X_GMISC0_IDSEL 0x01 /* Global control device ID select spca505 */
78 #define SPCA50X_GLOBAL_MISC0 0x00 /* Global control miscellaneous 0 spca505 */
80 #define SPCA50X_GLOBAL_MISC1 0x01 /* 505 */
81 #define SPCA50X_GLOBAL_MISC3 0x03 /* 505 */
82 #define SPCA50X_GMISC3_SAA7113RST 0x20 /* Not sure about this one spca505 */
84 /* Image format and compression control */
85 #define SPCA50X_REG_COMPRESS 0x04
88 * Data to initialize a SPCA505. Common to the CCD and external modes
90 static const u8 spca505_init_data
[][3] = {
91 /* bmRequest,value,index */
92 {SPCA50X_REG_GLOBAL
, SPCA50X_GMISC3_SAA7113RST
, SPCA50X_GLOBAL_MISC3
},
94 {SPCA50X_REG_GLOBAL
, 0x00, SPCA50X_GLOBAL_MISC3
},
95 {SPCA50X_REG_GLOBAL
, 0x00, SPCA50X_GLOBAL_MISC1
},
97 {SPCA50X_REG_GLOBAL
, SPCA50X_GMISC0_IDSEL
, SPCA50X_GLOBAL_MISC0
},
100 /* Maybe power down some stuff */
103 /* Setup internal CCD ? */
125 * Data to initialize the camera using the internal CCD
127 static const u8 spca505_open_data_ccd
[][3] = {
128 /* bmRequest,value,index */
129 /* Internal CCD data set */
131 /* This could be a reset */
134 /* Setup compression and image registers. 0x6 and 0x7 seem to be
135 related to H&V hold, and are resolution mode specific */
137 /* DIFF(0x50), was (0x10) */
144 /* DIFF (0x4a), was (0xa) */
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 */
171 /* DIFF not written */
173 /* DIFF not written */
176 /* DIFF (0x3,0x28,0x3) */
179 /* DIFF not written */
183 /* Extra block copied from init to hopefully ensure CCD is in a sane state */
201 /* End of extra block */
268 /* Compress = OFF (0x1 to turn on) */
274 /* was 5 (Image Type ? ) */
306 * Made by Tomasz Zablocki (skalamandra@poczta.onet.pl)
307 * SPCA505b chip based cameras initialization data
310 #define initial_brightness 0x7f /* 0x0(white)-0xff(black) */
311 /* #define initial_brightness 0x0 //0x0(white)-0xff(black) */
313 * Data to initialize a SPCA505. Common to the CCD and external modes
315 static const u8 spca505b_init_data
[][3] = {
317 {0x02, 0x00, 0x00}, /* init */
373 * Data to initialize the camera using the internal CCD
375 static const u8 spca505b_open_data_ccd
[][3] = {
377 /* {0x02,0x00,0x00}, */
378 {0x03, 0x04, 0x01}, /* rst */
404 {0x05, initial_brightness
>> 6, 0x00},
405 {0x05, (initial_brightness
<< 2) & 0xff, 0x01},
415 {0x05, 0x80, 0x14}, /* max exposure off (0=on) */
434 {0x06, 0x11, 0x10}, /* contrast */
499 {0x06, 0x4d, 0x51}, /* maybe saturation (4d) */
500 {0x06, 0x84, 0x53}, /* making green (84) */
501 {0x06, 0x00, 0x57}, /* sharpness (1) */
506 {0x06, 0x18, 0x0c}, /* maybe hue (18) */
510 {0x06, 0x18, 0x10}, /* maybe contrast (18) */
514 {0x04, 0x00, 0x08}, /* compression */
519 {0x04, 0x1d, 0x00}, /* imagetype (1d) */
520 {0x04, 0x41, 0x01}, /* hardware snapcontrol */
538 {0x05, initial_brightness
>> 6, 0x00},
539 {0x05, (initial_brightness
<< 2) & 0xff, 0x01},
547 static int reg_write(struct gspca_dev
*gspca_dev
,
548 u16 req
, u16 index
, u16 value
)
551 struct usb_device
*dev
= gspca_dev
->dev
;
553 ret
= usb_control_msg(dev
,
554 usb_sndctrlpipe(dev
, 0),
556 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
557 value
, index
, NULL
, 0, 500);
558 PDEBUG(D_USBO
, "reg write: 0x%02x,0x%02x:0x%02x, %d",
559 req
, index
, value
, ret
);
561 pr_err("reg write: error %d\n", ret
);
565 /* returns: negative is error, pos or zero is data */
566 static int reg_read(struct gspca_dev
*gspca_dev
,
567 u16 req
, /* bRequest */
568 u16 index
) /* wIndex */
572 ret
= usb_control_msg(gspca_dev
->dev
,
573 usb_rcvctrlpipe(gspca_dev
->dev
, 0),
575 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
578 gspca_dev
->usb_buf
, 2,
582 return (gspca_dev
->usb_buf
[1] << 8) + gspca_dev
->usb_buf
[0];
585 static int write_vector(struct gspca_dev
*gspca_dev
,
590 while (data
[i
][0] != 0) {
591 ret
= reg_write(gspca_dev
, data
[i
][0], data
[i
][2],
600 /* this function is called at probe time */
601 static int sd_config(struct gspca_dev
*gspca_dev
,
602 const struct usb_device_id
*id
)
604 struct sd
*sd
= (struct sd
*) gspca_dev
;
607 cam
= &gspca_dev
->cam
;
608 cam
->cam_mode
= vga_mode
;
609 sd
->subtype
= id
->driver_info
;
610 if (sd
->subtype
!= IntelPCCameraPro
)
611 cam
->nmodes
= ARRAY_SIZE(vga_mode
);
612 else /* no 640x480 for IntelPCCameraPro */
613 cam
->nmodes
= ARRAY_SIZE(vga_mode
) - 1;
618 /* this function is called at probe and resume time */
619 static int sd_init(struct gspca_dev
*gspca_dev
)
621 struct sd
*sd
= (struct sd
*) gspca_dev
;
623 if (write_vector(gspca_dev
,
624 sd
->subtype
== Nxultra
626 : spca505_init_data
))
631 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 brightness
)
633 reg_write(gspca_dev
, 0x05, 0x00, (255 - brightness
) >> 6);
634 reg_write(gspca_dev
, 0x05, 0x01, (255 - brightness
) << 2);
637 static int sd_start(struct gspca_dev
*gspca_dev
)
639 struct sd
*sd
= (struct sd
*) gspca_dev
;
641 static u8 mode_tb
[][3] = {
643 {0x00, 0x10, 0x10}, /* 640x480 */
644 {0x01, 0x1a, 0x1a}, /* 352x288 */
645 {0x02, 0x1c, 0x1d}, /* 320x240 */
646 {0x04, 0x34, 0x34}, /* 176x144 */
647 {0x05, 0x40, 0x40} /* 160x120 */
650 if (sd
->subtype
== Nxultra
)
651 write_vector(gspca_dev
, spca505b_open_data_ccd
);
653 write_vector(gspca_dev
, spca505_open_data_ccd
);
654 ret
= reg_read(gspca_dev
, 0x06, 0x16);
657 PERR("register read failed err: %d", ret
);
661 pr_err("After vector read returns 0x%04x should be 0x0101\n",
665 ret
= reg_write(gspca_dev
, 0x06, 0x16, 0x0a);
668 reg_write(gspca_dev
, 0x05, 0xc2, 0x12);
670 /* necessary because without it we can see stream
671 * only once after loading module */
672 /* stopping usb registers Tomasz change */
673 reg_write(gspca_dev
, 0x02, 0x00, 0x00);
675 mode
= gspca_dev
->cam
.cam_mode
[(int) gspca_dev
->curr_mode
].priv
;
676 reg_write(gspca_dev
, SPCA50X_REG_COMPRESS
, 0x00, mode_tb
[mode
][0]);
677 reg_write(gspca_dev
, SPCA50X_REG_COMPRESS
, 0x06, mode_tb
[mode
][1]);
678 reg_write(gspca_dev
, SPCA50X_REG_COMPRESS
, 0x07, mode_tb
[mode
][2]);
680 return reg_write(gspca_dev
, SPCA50X_REG_USB
,
682 SPCA50X_CUSB_ENABLE
);
685 static void sd_stopN(struct gspca_dev
*gspca_dev
)
687 /* Disable ISO packet machine */
688 reg_write(gspca_dev
, 0x02, 0x00, 0x00);
691 /* called on streamoff with alt 0 and on disconnect */
692 static void sd_stop0(struct gspca_dev
*gspca_dev
)
694 if (!gspca_dev
->present
)
697 /* This maybe reset or power control */
698 reg_write(gspca_dev
, 0x03, 0x03, 0x20);
699 reg_write(gspca_dev
, 0x03, 0x01, 0x00);
700 reg_write(gspca_dev
, 0x03, 0x00, 0x01);
701 reg_write(gspca_dev
, 0x05, 0x10, 0x01);
702 reg_write(gspca_dev
, 0x05, 0x11, 0x0f);
705 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
706 u8
*data
, /* isoc packet */
707 int len
) /* iso packet length */
710 case 0: /* start of frame */
711 gspca_frame_add(gspca_dev
, LAST_PACKET
, NULL
, 0);
712 data
+= SPCA50X_OFFSET_DATA
;
713 len
-= SPCA50X_OFFSET_DATA
;
714 gspca_frame_add(gspca_dev
, FIRST_PACKET
, data
, len
);
716 case 0xff: /* drop */
721 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
726 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
728 struct gspca_dev
*gspca_dev
=
729 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
731 gspca_dev
->usb_err
= 0;
733 if (!gspca_dev
->streaming
)
737 case V4L2_CID_BRIGHTNESS
:
738 setbrightness(gspca_dev
, ctrl
->val
);
741 return gspca_dev
->usb_err
;
744 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
748 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
750 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
752 gspca_dev
->vdev
.ctrl_handler
= hdl
;
753 v4l2_ctrl_handler_init(hdl
, 5);
754 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
755 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 127);
758 pr_err("Could not initialize controls\n");
764 /* sub-driver description */
765 static const struct sd_desc sd_desc
= {
768 .init_controls
= sd_init_controls
,
773 .pkt_scan
= sd_pkt_scan
,
776 /* -- module initialisation -- */
777 static const struct usb_device_id device_table
[] = {
778 {USB_DEVICE(0x041e, 0x401d), .driver_info
= Nxultra
},
779 {USB_DEVICE(0x0733, 0x0430), .driver_info
= IntelPCCameraPro
},
780 /*fixme: may be UsbGrabberPV321 BRIDGE_SPCA506 SENSOR_SAA7113 */
783 MODULE_DEVICE_TABLE(usb
, device_table
);
785 /* -- device connect -- */
786 static int sd_probe(struct usb_interface
*intf
,
787 const struct usb_device_id
*id
)
789 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
793 static struct usb_driver sd_driver
= {
795 .id_table
= device_table
,
797 .disconnect
= gspca_disconnect
,
799 .suspend
= gspca_suspend
,
800 .resume
= gspca_resume
,
801 .reset_resume
= gspca_resume
,
805 module_usb_driver(sd_driver
);