1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SPCA505 chip based cameras initialization data
5 * V4L2 by Jean-Francis Moine <http://moinejf.free.fr>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #define MODULE_NAME "spca505"
14 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
15 MODULE_DESCRIPTION("GSPCA/SPCA505 USB Camera Driver");
16 MODULE_LICENSE("GPL");
18 /* specific webcam descriptor */
20 struct gspca_dev gspca_dev
; /* !! must be the first item */
23 #define IntelPCCameraPro 0
27 static const struct v4l2_pix_format vga_mode
[] = {
28 {160, 120, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
30 .sizeimage
= 160 * 120 * 3 / 2,
31 .colorspace
= V4L2_COLORSPACE_SRGB
,
33 {176, 144, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
35 .sizeimage
= 176 * 144 * 3 / 2,
36 .colorspace
= V4L2_COLORSPACE_SRGB
,
38 {320, 240, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
40 .sizeimage
= 320 * 240 * 3 / 2,
41 .colorspace
= V4L2_COLORSPACE_SRGB
,
43 {352, 288, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
45 .sizeimage
= 352 * 288 * 3 / 2,
46 .colorspace
= V4L2_COLORSPACE_SRGB
,
48 {640, 480, V4L2_PIX_FMT_SPCA505
, V4L2_FIELD_NONE
,
50 .sizeimage
= 640 * 480 * 3 / 2,
51 .colorspace
= V4L2_COLORSPACE_SRGB
,
55 #define SPCA50X_OFFSET_DATA 10
57 #define SPCA50X_REG_USB 0x02 /* spca505 501 */
59 #define SPCA50X_USB_CTRL 0x00 /* spca505 */
60 #define SPCA50X_CUSB_ENABLE 0x01 /* spca505 */
62 #define SPCA50X_REG_GLOBAL 0x03 /* spca505 */
63 #define SPCA50X_GMISC0_IDSEL 0x01 /* Global control device ID select spca505 */
64 #define SPCA50X_GLOBAL_MISC0 0x00 /* Global control miscellaneous 0 spca505 */
66 #define SPCA50X_GLOBAL_MISC1 0x01 /* 505 */
67 #define SPCA50X_GLOBAL_MISC3 0x03 /* 505 */
68 #define SPCA50X_GMISC3_SAA7113RST 0x20 /* Not sure about this one spca505 */
70 /* Image format and compression control */
71 #define SPCA50X_REG_COMPRESS 0x04
74 * Data to initialize a SPCA505. Common to the CCD and external modes
76 static const u8 spca505_init_data
[][3] = {
77 /* bmRequest,value,index */
78 {SPCA50X_REG_GLOBAL
, SPCA50X_GMISC3_SAA7113RST
, SPCA50X_GLOBAL_MISC3
},
80 {SPCA50X_REG_GLOBAL
, 0x00, SPCA50X_GLOBAL_MISC3
},
81 {SPCA50X_REG_GLOBAL
, 0x00, SPCA50X_GLOBAL_MISC1
},
83 {SPCA50X_REG_GLOBAL
, SPCA50X_GMISC0_IDSEL
, SPCA50X_GLOBAL_MISC0
},
86 /* Maybe power down some stuff */
89 /* Setup internal CCD ? */
111 * Data to initialize the camera using the internal CCD
113 static const u8 spca505_open_data_ccd
[][3] = {
114 /* bmRequest,value,index */
115 /* Internal CCD data set */
117 /* This could be a reset */
120 /* Setup compression and image registers. 0x6 and 0x7 seem to be
121 related to H&V hold, and are resolution mode specific */
123 /* DIFF(0x50), was (0x10) */
130 /* DIFF (0x4a), was (0xa) */
135 /* DIFF not written */
137 /* DIFF not written */
139 /* DIFF not written */
141 /* DIFF not written */
143 /* DIFF not written */
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 */
162 /* DIFF (0x3,0x28,0x3) */
165 /* DIFF not written */
169 /* Extra block copied from init to hopefully ensure CCD is in a sane state */
187 /* End of extra block */
254 /* Compress = OFF (0x1 to turn on) */
260 /* was 5 (Image Type ? ) */
292 * Made by Tomasz Zablocki (skalamandra@poczta.onet.pl)
293 * SPCA505b chip based cameras initialization data
296 #define initial_brightness 0x7f /* 0x0(white)-0xff(black) */
297 /* #define initial_brightness 0x0 //0x0(white)-0xff(black) */
299 * Data to initialize a SPCA505. Common to the CCD and external modes
301 static const u8 spca505b_init_data
[][3] = {
303 {0x02, 0x00, 0x00}, /* init */
359 * Data to initialize the camera using the internal CCD
361 static const u8 spca505b_open_data_ccd
[][3] = {
363 /* {0x02,0x00,0x00}, */
364 {0x03, 0x04, 0x01}, /* rst */
390 {0x05, initial_brightness
>> 6, 0x00},
391 {0x05, (initial_brightness
<< 2) & 0xff, 0x01},
401 {0x05, 0x80, 0x14}, /* max exposure off (0=on) */
420 {0x06, 0x11, 0x10}, /* contrast */
485 {0x06, 0x4d, 0x51}, /* maybe saturation (4d) */
486 {0x06, 0x84, 0x53}, /* making green (84) */
487 {0x06, 0x00, 0x57}, /* sharpness (1) */
492 {0x06, 0x18, 0x0c}, /* maybe hue (18) */
496 {0x06, 0x18, 0x10}, /* maybe contrast (18) */
500 {0x04, 0x00, 0x08}, /* compression */
505 {0x04, 0x1d, 0x00}, /* imagetype (1d) */
506 {0x04, 0x41, 0x01}, /* hardware snapcontrol */
524 {0x05, initial_brightness
>> 6, 0x00},
525 {0x05, (initial_brightness
<< 2) & 0xff, 0x01},
533 static int reg_write(struct gspca_dev
*gspca_dev
,
534 u16 req
, u16 index
, u16 value
)
537 struct usb_device
*dev
= gspca_dev
->dev
;
539 ret
= usb_control_msg(dev
,
540 usb_sndctrlpipe(dev
, 0),
542 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
543 value
, index
, NULL
, 0, 500);
544 gspca_dbg(gspca_dev
, D_USBO
, "reg write: 0x%02x,0x%02x:0x%02x, %d\n",
545 req
, index
, value
, ret
);
547 pr_err("reg write: error %d\n", ret
);
551 /* returns: negative is error, pos or zero is data */
552 static int reg_read(struct gspca_dev
*gspca_dev
,
553 u16 req
, /* bRequest */
554 u16 index
) /* wIndex */
558 ret
= usb_control_msg(gspca_dev
->dev
,
559 usb_rcvctrlpipe(gspca_dev
->dev
, 0),
561 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
564 gspca_dev
->usb_buf
, 2,
568 return (gspca_dev
->usb_buf
[1] << 8) + gspca_dev
->usb_buf
[0];
571 static int write_vector(struct gspca_dev
*gspca_dev
,
576 while (data
[i
][0] != 0) {
577 ret
= reg_write(gspca_dev
, data
[i
][0], data
[i
][2],
586 /* this function is called at probe time */
587 static int sd_config(struct gspca_dev
*gspca_dev
,
588 const struct usb_device_id
*id
)
590 struct sd
*sd
= (struct sd
*) gspca_dev
;
593 cam
= &gspca_dev
->cam
;
594 cam
->cam_mode
= vga_mode
;
595 sd
->subtype
= id
->driver_info
;
596 if (sd
->subtype
!= IntelPCCameraPro
)
597 cam
->nmodes
= ARRAY_SIZE(vga_mode
);
598 else /* no 640x480 for IntelPCCameraPro */
599 cam
->nmodes
= ARRAY_SIZE(vga_mode
) - 1;
604 /* this function is called at probe and resume time */
605 static int sd_init(struct gspca_dev
*gspca_dev
)
607 struct sd
*sd
= (struct sd
*) gspca_dev
;
609 if (write_vector(gspca_dev
,
610 sd
->subtype
== Nxultra
612 : spca505_init_data
))
617 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 brightness
)
619 reg_write(gspca_dev
, 0x05, 0x00, (255 - brightness
) >> 6);
620 reg_write(gspca_dev
, 0x05, 0x01, (255 - brightness
) << 2);
623 static int sd_start(struct gspca_dev
*gspca_dev
)
625 struct sd
*sd
= (struct sd
*) gspca_dev
;
627 static u8 mode_tb
[][3] = {
629 {0x00, 0x10, 0x10}, /* 640x480 */
630 {0x01, 0x1a, 0x1a}, /* 352x288 */
631 {0x02, 0x1c, 0x1d}, /* 320x240 */
632 {0x04, 0x34, 0x34}, /* 176x144 */
633 {0x05, 0x40, 0x40} /* 160x120 */
636 if (sd
->subtype
== Nxultra
)
637 write_vector(gspca_dev
, spca505b_open_data_ccd
);
639 write_vector(gspca_dev
, spca505_open_data_ccd
);
640 ret
= reg_read(gspca_dev
, 0x06, 0x16);
643 gspca_err(gspca_dev
, "register read failed err: %d\n", ret
);
647 pr_err("After vector read returns 0x%04x should be 0x0101\n",
651 ret
= reg_write(gspca_dev
, 0x06, 0x16, 0x0a);
654 reg_write(gspca_dev
, 0x05, 0xc2, 0x12);
656 /* necessary because without it we can see stream
657 * only once after loading module */
658 /* stopping usb registers Tomasz change */
659 reg_write(gspca_dev
, 0x02, 0x00, 0x00);
661 mode
= gspca_dev
->cam
.cam_mode
[(int) gspca_dev
->curr_mode
].priv
;
662 reg_write(gspca_dev
, SPCA50X_REG_COMPRESS
, 0x00, mode_tb
[mode
][0]);
663 reg_write(gspca_dev
, SPCA50X_REG_COMPRESS
, 0x06, mode_tb
[mode
][1]);
664 reg_write(gspca_dev
, SPCA50X_REG_COMPRESS
, 0x07, mode_tb
[mode
][2]);
666 return reg_write(gspca_dev
, SPCA50X_REG_USB
,
668 SPCA50X_CUSB_ENABLE
);
671 static void sd_stopN(struct gspca_dev
*gspca_dev
)
673 /* Disable ISO packet machine */
674 reg_write(gspca_dev
, 0x02, 0x00, 0x00);
677 /* called on streamoff with alt 0 and on disconnect */
678 static void sd_stop0(struct gspca_dev
*gspca_dev
)
680 if (!gspca_dev
->present
)
683 /* This maybe reset or power control */
684 reg_write(gspca_dev
, 0x03, 0x03, 0x20);
685 reg_write(gspca_dev
, 0x03, 0x01, 0x00);
686 reg_write(gspca_dev
, 0x03, 0x00, 0x01);
687 reg_write(gspca_dev
, 0x05, 0x10, 0x01);
688 reg_write(gspca_dev
, 0x05, 0x11, 0x0f);
691 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
692 u8
*data
, /* isoc packet */
693 int len
) /* iso packet length */
696 case 0: /* start of frame */
697 gspca_frame_add(gspca_dev
, LAST_PACKET
, NULL
, 0);
698 data
+= SPCA50X_OFFSET_DATA
;
699 len
-= SPCA50X_OFFSET_DATA
;
700 gspca_frame_add(gspca_dev
, FIRST_PACKET
, data
, len
);
702 case 0xff: /* drop */
707 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
712 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
714 struct gspca_dev
*gspca_dev
=
715 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
717 gspca_dev
->usb_err
= 0;
719 if (!gspca_dev
->streaming
)
723 case V4L2_CID_BRIGHTNESS
:
724 setbrightness(gspca_dev
, ctrl
->val
);
727 return gspca_dev
->usb_err
;
730 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
734 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
736 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
738 gspca_dev
->vdev
.ctrl_handler
= hdl
;
739 v4l2_ctrl_handler_init(hdl
, 5);
740 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
741 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 127);
744 pr_err("Could not initialize controls\n");
750 /* sub-driver description */
751 static const struct sd_desc sd_desc
= {
754 .init_controls
= sd_init_controls
,
759 .pkt_scan
= sd_pkt_scan
,
762 /* -- module initialisation -- */
763 static const struct usb_device_id device_table
[] = {
764 {USB_DEVICE(0x041e, 0x401d), .driver_info
= Nxultra
},
765 {USB_DEVICE(0x0733, 0x0430), .driver_info
= IntelPCCameraPro
},
766 /*fixme: may be UsbGrabberPV321 BRIDGE_SPCA506 SENSOR_SAA7113 */
769 MODULE_DEVICE_TABLE(usb
, device_table
);
771 /* -- device connect -- */
772 static int sd_probe(struct usb_interface
*intf
,
773 const struct usb_device_id
*id
)
775 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
779 static struct usb_driver sd_driver
= {
781 .id_table
= device_table
,
783 .disconnect
= gspca_disconnect
,
785 .suspend
= gspca_suspend
,
786 .resume
= gspca_resume
,
787 .reset_resume
= gspca_resume
,
791 module_usb_driver(sd_driver
);