29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #define MODULE_NAME "cpia1"
33 #include <linux/input.h>
45 #define VIDEOSIZE_QCIF 0
46 #define VIDEOSIZE_CIF 1
47 #define SUBSAMPLE_420 0
48 #define SUBSAMPLE_422 1
49 #define YUVORDER_YUYV 0
50 #define YUVORDER_UYVY 1
51 #define NOT_COMPRESSED 0
53 #define NO_DECIMATION 0
54 #define DECIMATION_ENAB 1
57 #define FRAME_HEADER_SIZE 64
60 #define CPIA_GRAB_SINGLE 0
61 #define CPIA_GRAB_CONTINEOUS 1
64 #define CPIA_COMPRESSION_NONE 0
65 #define CPIA_COMPRESSION_AUTO 1
66 #define CPIA_COMPRESSION_MANUAL 2
67 #define CPIA_COMPRESSION_TARGET_QUALITY 0
68 #define CPIA_COMPRESSION_TARGET_FRAMERATE 1
81 #define UNINITIALISED_STATE 0
82 #define PASS_THROUGH_STATE 1
83 #define LO_POWER_STATE 2
84 #define HI_POWER_STATE 3
85 #define WARM_BOOT_STATE 4
93 #define STREAM_NOT_READY 0
94 #define STREAM_READY 1
96 #define STREAM_PAUSED 3
97 #define STREAM_FINISHED 4
101 #define SYSTEM_FLAG 2
102 #define INT_CTRL_FLAG 4
103 #define PROCESS_FLAG 8
105 #define VP_CTRL_FLAG 32
106 #define CAPTURE_FLAG 64
107 #define DEBUG_FLAG 128
110 #define VP_STATE_OK 0x00
112 #define VP_STATE_FAILED_VIDEOINIT 0x01
113 #define VP_STATE_FAILED_AECACBINIT 0x02
114 #define VP_STATE_AEC_MAX 0x04
115 #define VP_STATE_ACB_BMAX 0x08
117 #define VP_STATE_ACB_RMIN 0x10
118 #define VP_STATE_ACB_GMIN 0x20
119 #define VP_STATE_ACB_RMAX 0x40
120 #define VP_STATE_ACB_GMAX 0x80
124 #define COMP_GREEN1 214
125 #define COMP_GREEN2 COMP_GREEN1
126 #define COMP_BLUE 230
129 #define EXPOSURE_VERY_LIGHT 0
130 #define EXPOSURE_LIGHT 1
131 #define EXPOSURE_NORMAL 2
132 #define EXPOSURE_DARK 3
133 #define EXPOSURE_VERY_DARK 4
135 #define CPIA_MODULE_CPIA (0 << 5)
136 #define CPIA_MODULE_SYSTEM (1 << 5)
137 #define CPIA_MODULE_VP_CTRL (5 << 5)
138 #define CPIA_MODULE_CAPTURE (6 << 5)
139 #define CPIA_MODULE_DEBUG (7 << 5)
141 #define INPUT (DATA_IN << 8)
142 #define OUTPUT (DATA_OUT << 8)
144 #define CPIA_COMMAND_GetCPIAVersion (INPUT | CPIA_MODULE_CPIA | 1)
145 #define CPIA_COMMAND_GetPnPID (INPUT | CPIA_MODULE_CPIA | 2)
146 #define CPIA_COMMAND_GetCameraStatus (INPUT | CPIA_MODULE_CPIA | 3)
147 #define CPIA_COMMAND_GotoHiPower (OUTPUT | CPIA_MODULE_CPIA | 4)
148 #define CPIA_COMMAND_GotoLoPower (OUTPUT | CPIA_MODULE_CPIA | 5)
149 #define CPIA_COMMAND_GotoSuspend (OUTPUT | CPIA_MODULE_CPIA | 7)
150 #define CPIA_COMMAND_GotoPassThrough (OUTPUT | CPIA_MODULE_CPIA | 8)
151 #define CPIA_COMMAND_ModifyCameraStatus (OUTPUT | CPIA_MODULE_CPIA | 10)
153 #define CPIA_COMMAND_ReadVCRegs (INPUT | CPIA_MODULE_SYSTEM | 1)
154 #define CPIA_COMMAND_WriteVCReg (OUTPUT | CPIA_MODULE_SYSTEM | 2)
155 #define CPIA_COMMAND_ReadMCPorts (INPUT | CPIA_MODULE_SYSTEM | 3)
156 #define CPIA_COMMAND_WriteMCPort (OUTPUT | CPIA_MODULE_SYSTEM | 4)
157 #define CPIA_COMMAND_SetBaudRate (OUTPUT | CPIA_MODULE_SYSTEM | 5)
158 #define CPIA_COMMAND_SetECPTiming (OUTPUT | CPIA_MODULE_SYSTEM | 6)
159 #define CPIA_COMMAND_ReadIDATA (INPUT | CPIA_MODULE_SYSTEM | 7)
160 #define CPIA_COMMAND_WriteIDATA (OUTPUT | CPIA_MODULE_SYSTEM | 8)
161 #define CPIA_COMMAND_GenericCall (OUTPUT | CPIA_MODULE_SYSTEM | 9)
162 #define CPIA_COMMAND_I2CStart (OUTPUT | CPIA_MODULE_SYSTEM | 10)
163 #define CPIA_COMMAND_I2CStop (OUTPUT | CPIA_MODULE_SYSTEM | 11)
164 #define CPIA_COMMAND_I2CWrite (OUTPUT | CPIA_MODULE_SYSTEM | 12)
165 #define CPIA_COMMAND_I2CRead (INPUT | CPIA_MODULE_SYSTEM | 13)
167 #define CPIA_COMMAND_GetVPVersion (INPUT | CPIA_MODULE_VP_CTRL | 1)
168 #define CPIA_COMMAND_ResetFrameCounter (INPUT | CPIA_MODULE_VP_CTRL | 2)
169 #define CPIA_COMMAND_SetColourParams (OUTPUT | CPIA_MODULE_VP_CTRL | 3)
170 #define CPIA_COMMAND_SetExposure (OUTPUT | CPIA_MODULE_VP_CTRL | 4)
171 #define CPIA_COMMAND_SetColourBalance (OUTPUT | CPIA_MODULE_VP_CTRL | 6)
172 #define CPIA_COMMAND_SetSensorFPS (OUTPUT | CPIA_MODULE_VP_CTRL | 7)
173 #define CPIA_COMMAND_SetVPDefaults (OUTPUT | CPIA_MODULE_VP_CTRL | 8)
174 #define CPIA_COMMAND_SetApcor (OUTPUT | CPIA_MODULE_VP_CTRL | 9)
175 #define CPIA_COMMAND_SetFlickerCtrl (OUTPUT | CPIA_MODULE_VP_CTRL | 10)
176 #define CPIA_COMMAND_SetVLOffset (OUTPUT | CPIA_MODULE_VP_CTRL | 11)
177 #define CPIA_COMMAND_GetColourParams (INPUT | CPIA_MODULE_VP_CTRL | 16)
178 #define CPIA_COMMAND_GetColourBalance (INPUT | CPIA_MODULE_VP_CTRL | 17)
179 #define CPIA_COMMAND_GetExposure (INPUT | CPIA_MODULE_VP_CTRL | 18)
180 #define CPIA_COMMAND_SetSensorMatrix (OUTPUT | CPIA_MODULE_VP_CTRL | 19)
181 #define CPIA_COMMAND_ColourBars (OUTPUT | CPIA_MODULE_VP_CTRL | 25)
182 #define CPIA_COMMAND_ReadVPRegs (INPUT | CPIA_MODULE_VP_CTRL | 30)
183 #define CPIA_COMMAND_WriteVPReg (OUTPUT | CPIA_MODULE_VP_CTRL | 31)
185 #define CPIA_COMMAND_GrabFrame (OUTPUT | CPIA_MODULE_CAPTURE | 1)
186 #define CPIA_COMMAND_UploadFrame (OUTPUT | CPIA_MODULE_CAPTURE | 2)
187 #define CPIA_COMMAND_SetGrabMode (OUTPUT | CPIA_MODULE_CAPTURE | 3)
188 #define CPIA_COMMAND_InitStreamCap (OUTPUT | CPIA_MODULE_CAPTURE | 4)
189 #define CPIA_COMMAND_FiniStreamCap (OUTPUT | CPIA_MODULE_CAPTURE | 5)
190 #define CPIA_COMMAND_StartStreamCap (OUTPUT | CPIA_MODULE_CAPTURE | 6)
191 #define CPIA_COMMAND_EndStreamCap (OUTPUT | CPIA_MODULE_CAPTURE | 7)
192 #define CPIA_COMMAND_SetFormat (OUTPUT | CPIA_MODULE_CAPTURE | 8)
193 #define CPIA_COMMAND_SetROI (OUTPUT | CPIA_MODULE_CAPTURE | 9)
194 #define CPIA_COMMAND_SetCompression (OUTPUT | CPIA_MODULE_CAPTURE | 10)
195 #define CPIA_COMMAND_SetCompressionTarget (OUTPUT | CPIA_MODULE_CAPTURE | 11)
196 #define CPIA_COMMAND_SetYUVThresh (OUTPUT | CPIA_MODULE_CAPTURE | 12)
197 #define CPIA_COMMAND_SetCompressionParams (OUTPUT | CPIA_MODULE_CAPTURE | 13)
198 #define CPIA_COMMAND_DiscardFrame (OUTPUT | CPIA_MODULE_CAPTURE | 14)
199 #define CPIA_COMMAND_GrabReset (OUTPUT | CPIA_MODULE_CAPTURE | 15)
201 #define CPIA_COMMAND_OutputRS232 (OUTPUT | CPIA_MODULE_DEBUG | 1)
202 #define CPIA_COMMAND_AbortProcess (OUTPUT | CPIA_MODULE_DEBUG | 4)
203 #define CPIA_COMMAND_SetDramPage (OUTPUT | CPIA_MODULE_DEBUG | 5)
204 #define CPIA_COMMAND_StartDramUpload (OUTPUT | CPIA_MODULE_DEBUG | 6)
205 #define CPIA_COMMAND_StartDummyDtream (OUTPUT | CPIA_MODULE_DEBUG | 8)
206 #define CPIA_COMMAND_AbortStream (OUTPUT | CPIA_MODULE_DEBUG | 9)
207 #define CPIA_COMMAND_DownloadDRAM (OUTPUT | CPIA_MODULE_DEBUG | 10)
208 #define CPIA_COMMAND_Null (OUTPUT | CPIA_MODULE_DEBUG | 11)
210 #define ROUND_UP_EXP_FOR_FLICKER 15
214 #define MAX_EXP_102 255
216 #define VERY_LOW_EXP 70
218 #define EXP_ACC_DARK 50
219 #define EXP_ACC_LIGHT 90
220 #define HIGH_COMP_102 160
225 #define FIRMWARE_VERSION(x, y) (sd->params.version.firmwareVersion == (x) && \
226 sd->params.version.firmwareRevision == (y))
228 #define CPIA1_CID_COMP_TARGET (V4L2_CTRL_CLASS_USER + 0x1000)
229 #define BRIGHTNESS_DEF 50
230 #define CONTRAST_DEF 48
231 #define SATURATION_DEF 50
232 #define FREQ_DEF V4L2_CID_POWER_LINE_FREQUENCY_50HZ
233 #define ILLUMINATORS_1_DEF 0
234 #define ILLUMINATORS_2_DEF 0
235 #define COMP_TARGET_DEF CPIA_COMPRESSION_TARGET_QUALITY
239 static u8 flicker_jumps[2][2][4] =
240 { { { 76, 38, 19, 9 }, { 92, 46, 23, 11 } },
241 { { 64, 32, 16, 8 }, { 76, 38, 19, 9} }
413 int ret, databytes = command[6] | (command[7] << 8);
418 pipe = usb_rcvctrlpipe(gspca_dev->
dev, 0);
420 }
else if (command[0] ==
DATA_OUT) {
421 pipe = usb_sndctrlpipe(gspca_dev->
dev, 0);
424 PDEBUG(
D_ERR,
"Unexpected first byte of command: %x",
433 command[2] | (command[3] << 8),
434 command[4] | (command[5] << 8),
435 gspca_dev->
usb_buf, databytes, 1000);
438 pr_err(
"usb_control_msg %02x, error %d\n", command[1], ret);
440 if (ret == -
EPIPE && retries > 0) {
445 return (ret < 0) ? ret : 0;
449 static int do_command(
struct gspca_dev *gspca_dev,
u16 command,
452 struct sd *
sd = (
struct sd *) gspca_dev;
475 cmd[0] = command >> 8;
476 cmd[1] = command & 0xff;
484 ret = cpia_usb_transferCmd(gspca_dev, cmd);
498 sd->
params.pnpID.product =
500 sd->
params.pnpID.deviceRevision =
516 sd->
params.vpVersion.cameraHeadID =
542 a = ((gspca_dev->
usb_buf[1] & 0x02) == 0);
543 if (a != sd->
params.qx3.button) {
544 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
545 input_report_key(gspca_dev->input_dev,
KEY_CAMERA, a);
546 input_sync(gspca_dev->input_dev);
550 if (sd->
params.qx3.button) {
559 sd->
params.qx3.cradled = ((gspca_dev->
usb_buf[2] & 0x40) == 0);
567 static int do_command_extended(
struct gspca_dev *gspca_dev,
u16 command,
574 cmd[0] = command >> 8;
575 cmd[1] = command & 0xff;
591 return cpia_usb_transferCmd(gspca_dev, cmd);
603 #define FLICKER_MAX_EXPOSURE 250
604 #define FLICKER_ALLOWABLE_OVER_EXPOSURE 146
605 #define FLICKER_BRIGHTNESS_CONSTANT 59
608 int MaxAllowableOverExposure, OverExposure;
614 OverExposure = MaxAllowableOverExposure;
620 #undef FLICKER_MAX_EXPOSURE
621 #undef FLICKER_ALLOWABLE_OVER_EXPOSURE
622 #undef FLICKER_BRIGHTNESS_CONSTANT
625 static void reset_camera_params(
struct gspca_dev *gspca_dev)
627 struct sd *sd = (
struct sd *) gspca_dev;
652 params->
apcor.gain1 = 0x18;
653 params->
apcor.gain2 = 0x16;
654 params->
apcor.gain4 = 0x24;
655 params->
apcor.gain8 = 0x34;
697 params->
qx3.qx3_detected = 0;
698 params->
qx3.toplight = 0;
699 params->
qx3.bottomlight = 0;
700 params->
qx3.button = 0;
701 params->
qx3.cradled = 0;
706 PDEBUG(D_PROBE,
"status: %02x %02x %02x %02x %02x %02x %02x %02x",
713 static int goto_low_power(
struct gspca_dev *gspca_dev)
715 struct sd *sd = (
struct sd *) gspca_dev;
729 "unexpected state after lo power cmd: %02x",
730 sd->
params.status.systemState);
736 PDEBUG(D_CONF,
"camera now in LOW power state");
740 static int goto_high_power(
struct gspca_dev *gspca_dev)
742 struct sd *sd = (
struct sd *) gspca_dev;
759 PDEBUG(
D_ERR,
"unexpected state after hi power cmd: %02x",
760 sd->
params.status.systemState);
765 PDEBUG(D_CONF,
"camera now in HIGH power state");
769 static int get_version_information(
struct gspca_dev *gspca_dev)
782 static int save_camera_state(
struct gspca_dev *gspca_dev)
793 static int command_setformat(
struct gspca_dev *gspca_dev)
795 struct sd *sd = (
struct sd *) gspca_dev;
799 sd->
params.format.videoSize,
800 sd->
params.format.subSample,
801 sd->
params.format.yuvOrder, 0);
810 static int command_setcolourparams(
struct gspca_dev *gspca_dev)
812 struct sd *sd = (
struct sd *) gspca_dev;
814 sd->
params.colourParams.brightness,
815 sd->
params.colourParams.contrast,
816 sd->
params.colourParams.saturation, 0);
819 static int command_setapcor(
struct gspca_dev *gspca_dev)
821 struct sd *sd = (
struct sd *) gspca_dev;
829 static int command_setvloffset(
struct gspca_dev *gspca_dev)
831 struct sd *sd = (
struct sd *) gspca_dev;
833 sd->
params.vlOffset.gain1,
834 sd->
params.vlOffset.gain2,
835 sd->
params.vlOffset.gain4,
836 sd->
params.vlOffset.gain8);
839 static int command_setexposure(
struct gspca_dev *gspca_dev)
841 struct sd *sd = (
struct sd *) gspca_dev;
845 sd->
params.exposure.gainMode,
847 sd->
params.exposure.compMode,
848 sd->
params.exposure.centreWeight,
850 sd->
params.exposure.fineExp,
851 sd->
params.exposure.coarseExpLo,
852 sd->
params.exposure.coarseExpHi,
853 sd->
params.exposure.redComp,
854 sd->
params.exposure.green1Comp,
855 sd->
params.exposure.green2Comp,
856 sd->
params.exposure.blueComp);
860 if (sd->
params.exposure.expMode != 1) {
863 sd->
params.exposure.expMode,
866 sd->
params.exposure.fineExp,
867 sd->
params.exposure.coarseExpLo,
868 sd->
params.exposure.coarseExpHi,
875 static int command_setcolourbalance(
struct gspca_dev *gspca_dev)
877 struct sd *sd = (
struct sd *) gspca_dev;
879 if (sd->
params.colourBalance.balanceMode == 1) {
884 sd->
params.colourBalance.redGain,
885 sd->
params.colourBalance.greenGain,
886 sd->
params.colourBalance.blueGain);
893 if (sd->
params.colourBalance.balanceMode == 2) {
897 if (sd->
params.colourBalance.balanceMode == 3) {
905 static int command_setcompressiontarget(
struct gspca_dev *gspca_dev)
907 struct sd *sd = (
struct sd *) gspca_dev;
910 sd->
params.compressionTarget.frTargeting,
911 sd->
params.compressionTarget.targetFR,
912 sd->
params.compressionTarget.targetQ, 0);
915 static int command_setyuvtresh(
struct gspca_dev *gspca_dev)
917 struct sd *sd = (
struct sd *) gspca_dev;
920 sd->
params.yuvThreshold.yThreshold,
921 sd->
params.yuvThreshold.uvThreshold, 0, 0);
924 static int command_setcompressionparams(
struct gspca_dev *gspca_dev)
926 struct sd *sd = (
struct sd *) gspca_dev;
928 return do_command_extended(gspca_dev,
931 sd->
params.compressionParams.hysteresis,
932 sd->
params.compressionParams.threshMax,
933 sd->
params.compressionParams.smallStep,
934 sd->
params.compressionParams.largeStep,
935 sd->
params.compressionParams.decimationHysteresis,
936 sd->
params.compressionParams.frDiffStepThresh,
937 sd->
params.compressionParams.qDiffStepThresh,
938 sd->
params.compressionParams.decimationThreshMod);
941 static int command_setcompression(
struct gspca_dev *gspca_dev)
943 struct sd *sd = (
struct sd *) gspca_dev;
946 sd->
params.compression.mode,
947 sd->
params.compression.decimation, 0, 0);
950 static int command_setsensorfps(
struct gspca_dev *gspca_dev)
952 struct sd *sd = (
struct sd *) gspca_dev;
955 sd->
params.sensorFps.divisor,
956 sd->
params.sensorFps.baserate, 0, 0);
959 static int command_setflickerctrl(
struct gspca_dev *gspca_dev)
961 struct sd *sd = (
struct sd *) gspca_dev;
964 sd->
params.flickerControl.flickerMode,
965 sd->
params.flickerControl.coarseJump,
966 sd->
params.flickerControl.allowableOverExposure,
970 static int command_setecptiming(
struct gspca_dev *gspca_dev)
972 struct sd *sd = (
struct sd *) gspca_dev;
975 sd->
params.ecpTiming, 0, 0, 0);
978 static int command_pause(
struct gspca_dev *gspca_dev)
983 static int command_resume(
struct gspca_dev *gspca_dev)
985 struct sd *sd = (
struct sd *) gspca_dev;
988 0, sd->
params.streamStartLine, 0, 0);
991 static int command_setlights(
struct gspca_dev *gspca_dev)
993 struct sd *sd = (
struct sd *) gspca_dev;
996 p1 = (sd->
params.qx3.bottomlight == 0) << 1;
997 p2 = (sd->
params.qx3.toplight == 0) << 3;
1000 0x90, 0x8f, 0x50, 0);
1008 static int set_flicker(
struct gspca_dev *gspca_dev,
int on,
int apply)
1013 #define COMPGAIN(base, curexp, newexp) \
1014 (u8) ((((float) base - 128.0) * ((float) curexp / (float) newexp)) + 128.5)
1015 #define EXP_FROM_COMP(basecomp, curcomp, curexp) \
1016 (u16)((float)curexp * (float)(u8)(curcomp + 128) / \
1017 (float)(u8)(basecomp - 128))
1020 #define COMPGAIN(base, curexp, newexp) \
1021 (u8)(128 + (((u32)(2*(base-128)*curexp + newexp)) / (2 * newexp)))
1022 #define EXP_FROM_COMP(basecomp, curcomp, curexp) \
1023 (u16)(((u32)(curexp * (u8)(curcomp + 128)) / (u8)(basecomp - 128)))
1026 struct sd *sd = (
struct sd *) gspca_dev;
1027 int currentexp = sd->
params.exposure.coarseExpLo +
1028 sd->
params.exposure.coarseExpHi * 256;
1032 int cj = sd->
params.flickerControl.coarseJump;
1033 sd->
params.flickerControl.flickerMode = 1;
1034 sd->
params.flickerControl.disabled = 0;
1035 if (sd->
params.exposure.expMode != 2) {
1036 sd->
params.exposure.expMode = 2;
1039 currentexp = currentexp << sd->
params.exposure.gain;
1040 sd->
params.exposure.gain = 0;
1045 startexp = (startexp * cj) - 1;
1052 sd->
params.exposure.coarseExpLo = startexp & 0xff;
1053 sd->
params.exposure.coarseExpHi = startexp >> 8;
1054 if (currentexp > startexp) {
1055 if (currentexp > (2 * startexp))
1056 currentexp = 2 * startexp;
1057 sd->
params.exposure.redComp =
1059 sd->
params.exposure.green1Comp =
1061 sd->
params.exposure.green2Comp =
1063 sd->
params.exposure.blueComp =
1072 sd->
params.exposure.compMode = 0;
1074 sd->
params.exposure.compMode = 1;
1076 sd->
params.apcor.gain1 = 0x18;
1077 sd->
params.apcor.gain2 = 0x18;
1078 sd->
params.apcor.gain4 = 0x16;
1079 sd->
params.apcor.gain8 = 0x14;
1081 sd->
params.flickerControl.flickerMode = 0;
1082 sd->
params.flickerControl.disabled = 1;
1085 sd->
params.exposure.redComp, currentexp);
1087 sd->
params.exposure.green1Comp, currentexp);
1089 sd->
params.exposure.green2Comp, currentexp);
1091 sd->
params.exposure.blueComp, currentexp);
1092 startexp = startexp >> 2;
1094 sd->
params.exposure.gainMode - 1) {
1095 startexp = startexp >> 1;
1096 ++sd->
params.exposure.gain;
1102 sd->
params.exposure.coarseExpLo = startexp & 0xff;
1103 sd->
params.exposure.coarseExpHi = startexp >> 8;
1108 sd->
params.exposure.compMode = 1;
1109 sd->
params.apcor.gain1 = 0x18;
1110 sd->
params.apcor.gain2 = 0x16;
1111 sd->
params.apcor.gain4 = 0x24;
1112 sd->
params.apcor.gain8 = 0x34;
1114 sd->
params.vlOffset.gain1 = 20;
1115 sd->
params.vlOffset.gain2 = 24;
1116 sd->
params.vlOffset.gain4 = 26;
1117 sd->
params.vlOffset.gain8 = 26;
1120 ret = command_setexposure(gspca_dev);
1124 ret = command_setapcor(gspca_dev);
1128 ret = command_setvloffset(gspca_dev);
1132 ret = command_setflickerctrl(gspca_dev);
1138 #undef EXP_FROM_COMP
1143 static void monitor_exposure(
struct gspca_dev *gspca_dev)
1145 struct sd *sd = (
struct sd *) gspca_dev;
1146 u8 exp_acc, bcomp, cmd[8];
1147 int ret, light_exp, dark_exp, very_dark_exp;
1148 int old_exposure, new_exposure,
framerate;
1149 int setfps = 0, setexp = 0, setflicker = 0;
1161 ret = cpia_usb_transferCmd(gspca_dev, cmd);
1163 pr_err(
"ReadVPRegs(30,4,9,8) - failed: %d\n", ret);
1166 exp_acc = gspca_dev->
usb_buf[0];
1167 bcomp = gspca_dev->
usb_buf[1];
1169 light_exp = sd->
params.colourParams.brightness +
1171 if (light_exp > 255)
1173 dark_exp = sd->
params.colourParams.brightness +
1177 very_dark_exp = dark_exp / 2;
1179 old_exposure = sd->
params.exposure.coarseExpHi * 256 +
1180 sd->
params.exposure.coarseExpLo;
1182 if (!sd->
params.flickerControl.disabled) {
1187 if (bcomp >= max_comp && exp_acc < dark_exp) {
1189 if (exp_acc < very_dark_exp) {
1207 }
else if (old_exposure <= LOW_EXP || exp_acc > light_exp) {
1233 if (old_exposure >=
MAX_EXP && exp_acc < dark_exp) {
1235 if (exp_acc < very_dark_exp) {
1253 }
else if (old_exposure <= LOW_EXP || exp_acc > light_exp) {
1280 if (framerate > 30 || framerate < 1)
1283 if (!sd->
params.flickerControl.disabled) {
1288 sd->
params.sensorFps.divisor < 2) {
1291 ++sd->
params.sensorFps.divisor;
1294 sd->
params.flickerControl.coarseJump =
1296 [sd->
params.sensorFps.baserate]
1297 [sd->
params.sensorFps.divisor];
1300 new_exposure = sd->
params.flickerControl.coarseJump-1;
1301 while (new_exposure < old_exposure / 2)
1303 sd->
params.flickerControl.coarseJump;
1304 sd->
params.exposure.coarseExpLo = new_exposure & 0xff;
1305 sd->
params.exposure.coarseExpHi = new_exposure >> 8;
1308 PDEBUG(D_CONF,
"Automatically decreasing sensor_fps");
1313 sd->
params.sensorFps.divisor > 0) {
1318 --sd->
params.sensorFps.divisor;
1321 sd->
params.flickerControl.coarseJump =
1323 [sd->
params.sensorFps.baserate]
1324 [sd->
params.sensorFps.divisor];
1327 new_exposure = sd->
params.flickerControl.coarseJump-1;
1328 while (new_exposure < 2 * old_exposure &&
1330 sd->
params.flickerControl.coarseJump < max_exp)
1332 sd->
params.flickerControl.coarseJump;
1333 sd->
params.exposure.coarseExpLo = new_exposure & 0xff;
1334 sd->
params.exposure.coarseExpHi = new_exposure >> 8;
1337 PDEBUG(D_CONF,
"Automatically increasing sensor_fps");
1344 sd->
params.sensorFps.divisor < 2) {
1347 ++sd->
params.sensorFps.divisor;
1350 if (sd->
params.exposure.gain > 0) {
1351 --sd->
params.exposure.gain;
1355 PDEBUG(D_CONF,
"Automatically decreasing sensor_fps");
1360 sd->
params.sensorFps.divisor > 0) {
1363 --sd->
params.sensorFps.divisor;
1366 if (sd->
params.exposure.gain <
1367 sd->
params.exposure.gainMode - 1) {
1368 ++sd->
params.exposure.gain;
1372 PDEBUG(D_CONF,
"Automatically increasing sensor_fps");
1377 command_setexposure(gspca_dev);
1380 command_setsensorfps(gspca_dev);
1383 command_setflickerctrl(gspca_dev);
1394 static void restart_flicker(
struct gspca_dev *gspca_dev)
1396 struct sd *sd = (
struct sd *) gspca_dev;
1404 if (sd->
params.flickerControl.flickerMode == 0 ||
1408 old_exp = sd->
params.exposure.coarseExpLo +
1409 sd->
params.exposure.coarseExpHi*256;
1414 cam_exposure %= sd->
params.flickerControl.coarseJump;
1415 if (!sd->
params.flickerControl.disabled &&
1416 cam_exposure <= sd->
params.flickerControl.coarseJump - 3) {
1418 sd->
params.flickerControl.disabled = 1;
1421 if (sd->
params.flickerControl.disabled &&
1422 old_exp > sd->
params.flickerControl.coarseJump +
1426 set_flicker(gspca_dev, 1, 1);
1431 static int sd_config(
struct gspca_dev *gspca_dev,
1434 struct sd *sd = (
struct sd *) gspca_dev;
1438 reset_camera_params(gspca_dev);
1440 PDEBUG(D_PROBE,
"cpia CPiA camera detected (vid/pid 0x%04X:0x%04X)",
1443 cam = &gspca_dev->
cam;
1447 goto_low_power(gspca_dev);
1449 sd->
params.version.firmwareVersion = 0;
1450 get_version_information(gspca_dev);
1451 if (sd->
params.version.firmwareVersion != 1) {
1452 PDEBUG(
D_ERR,
"only firmware version 1 is supported (got: %d)",
1453 sd->
params.version.firmwareVersion);
1458 if (sd->
params.version.firmwareRevision <= 2 &&
1459 sd->
params.exposure.gainMode > 2) {
1460 sd->
params.exposure.gainMode = 2;
1464 sd->
params.qx3.qx3_detected = (sd->
params.pnpID.vendor == 0x0813 &&
1465 sd->
params.pnpID.product == 0x0001);
1470 static int sd_start(
struct gspca_dev *gspca_dev)
1472 struct sd *sd = (
struct sd *) gspca_dev;
1476 if (goto_low_power(gspca_dev)) {
1479 sd->
params.status.systemState);
1480 printstatus(&sd->
params);
1485 ret = goto_high_power(gspca_dev);
1494 ret = goto_low_power(gspca_dev);
1502 sd->
params.version.firmwareVersion = 0;
1503 get_version_information(gspca_dev);
1516 ret = goto_high_power(gspca_dev);
1525 if (sd->
params.status.fatalError) {
1526 PDEBUG(
D_ERR,
"fatal_error: %04x, vp_status: %04x",
1527 sd->
params.status.fatalError,
1528 sd->
params.status.vpStatus);
1539 sd->
params.streamStartLine = 120;
1541 priv = gspca_dev->
cam.cam_mode[gspca_dev->
curr_mode].priv;
1543 sd->
params.roi.colStart = 2;
1544 sd->
params.roi.rowStart = 6;
1546 sd->
params.roi.colStart = 0;
1547 sd->
params.roi.rowStart = 0;
1552 sd->
params.roi.colStart /= 2;
1553 sd->
params.roi.rowStart /= 2;
1554 sd->
params.streamStartLine /= 2;
1559 (gspca_dev->
width >> 3);
1561 (gspca_dev->
height >> 2);
1575 ret = command_setcompressiontarget(gspca_dev);
1578 ret = command_setcolourparams(gspca_dev);
1581 ret = command_setformat(gspca_dev);
1584 ret = command_setyuvtresh(gspca_dev);
1587 ret = command_setecptiming(gspca_dev);
1590 ret = command_setcompressionparams(gspca_dev);
1593 ret = command_setexposure(gspca_dev);
1596 ret = command_setcolourbalance(gspca_dev);
1599 ret = command_setsensorfps(gspca_dev);
1602 ret = command_setapcor(gspca_dev);
1605 ret = command_setflickerctrl(gspca_dev);
1608 ret = command_setvloffset(gspca_dev);
1613 ret = command_resume(gspca_dev);
1628 static void sd_stopN(
struct gspca_dev *gspca_dev)
1630 struct sd *sd = (
struct sd *) gspca_dev;
1632 command_pause(gspca_dev);
1635 save_camera_state(gspca_dev);
1638 goto_low_power(gspca_dev);
1643 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
1645 if (sd->
params.qx3.button) {
1649 input_report_key(gspca_dev->input_dev,
KEY_CAMERA, 0);
1650 input_sync(gspca_dev->input_dev);
1656 static int sd_init(
struct gspca_dev *gspca_dev)
1658 struct sd *sd = (
struct sd *) gspca_dev;
1664 ret = sd_start(gspca_dev);
1670 if (sd->
params.qx3.qx3_detected)
1671 command_setlights(gspca_dev);
1673 sd_stopN(gspca_dev);
1675 PDEBUG(D_PROBE,
"CPIA Version: %d.%02d (%d.%d)",
1676 sd->
params.version.firmwareVersion,
1677 sd->
params.version.firmwareRevision,
1678 sd->
params.version.vcVersion,
1679 sd->
params.version.vcRevision);
1680 PDEBUG(D_PROBE,
"CPIA PnP-ID: %04x:%04x:%04x",
1682 sd->
params.pnpID.deviceRevision);
1683 PDEBUG(D_PROBE,
"VP-Version: %d.%d %04x",
1684 sd->
params.vpVersion.vpVersion,
1685 sd->
params.vpVersion.vpRevision,
1686 sd->
params.vpVersion.cameraHeadID);
1691 static void sd_pkt_scan(
struct gspca_dev *gspca_dev,
1695 struct sd *sd = (
struct sd *) gspca_dev;
1700 data[16] == sd->
params.format.videoSize &&
1701 data[17] == sd->
params.format.subSample &&
1702 data[18] == sd->
params.format.yuvOrder &&
1703 data[24] == sd->
params.roi.colStart &&
1704 data[25] == sd->
params.roi.colEnd &&
1705 data[26] == sd->
params.roi.rowStart &&
1706 data[27] == sd->
params.roi.rowEnd) {
1713 image = gspca_dev->
image;
1714 if (image !=
NULL &&
1716 image[gspca_dev->
image_len - 4] == 0xff &&
1717 image[gspca_dev->
image_len - 3] == 0xff &&
1718 image[gspca_dev->
image_len - 2] == 0xff &&
1719 image[gspca_dev->
image_len - 1] == 0xff)
1730 static void sd_dq_callback(
struct gspca_dev *gspca_dev)
1732 struct sd *sd = (
struct sd *) gspca_dev;
1739 command_setcompression(gspca_dev);
1743 restart_flicker(gspca_dev);
1747 if (sd->
params.exposure.expMode == 2)
1748 monitor_exposure(gspca_dev);
1757 struct gspca_dev *gspca_dev =
1759 struct sd *sd = (
struct sd *)gspca_dev;
1768 sd->
params.colourParams.brightness = ctrl->
val;
1769 sd->
params.flickerControl.allowableOverExposure =
1770 find_over_exposure(sd->
params.colourParams.brightness);
1771 gspca_dev->
usb_err = command_setcolourparams(gspca_dev);
1773 gspca_dev->
usb_err = command_setflickerctrl(gspca_dev);
1776 sd->
params.colourParams.contrast = ctrl->
val;
1777 gspca_dev->
usb_err = command_setcolourparams(gspca_dev);
1780 sd->
params.colourParams.saturation = ctrl->
val;
1781 gspca_dev->
usb_err = command_setcolourparams(gspca_dev);
1785 sd->
params.flickerControl.coarseJump =
1787 [sd->
params.sensorFps.baserate]
1788 [sd->
params.sensorFps.divisor];
1790 gspca_dev->
usb_err = set_flicker(gspca_dev,
1796 gspca_dev->
usb_err = command_setlights(gspca_dev);
1800 gspca_dev->
usb_err = command_setlights(gspca_dev);
1803 sd->
params.compressionTarget.frTargeting = ctrl->
val;
1804 gspca_dev->
usb_err = command_setcompressiontarget(gspca_dev);
1811 .s_ctrl = sd_s_ctrl,
1814 static int sd_init_controls(
struct gspca_dev *gspca_dev)
1816 struct sd *sd = (
struct sd *)gspca_dev;
1818 static const char *
const comp_target_menu[] = {
1824 .
ops = &sd_ctrl_ops,
1827 .name =
"Compression Target",
1828 .qmenu = comp_target_menu,
1833 gspca_dev->
vdev.ctrl_handler = hdl;
1845 if (sd->
params.qx3.qx3_detected) {
1856 pr_err(
"Could not initialize controls\n");
1865 .config = sd_config,
1867 .init_controls = sd_init_controls,
1870 .dq_callback = sd_dq_callback,
1871 .pkt_scan = sd_pkt_scan,
1872 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
1879 {USB_DEVICE(0x0553, 0x0002)},
1880 {USB_DEVICE(0x0813, 0x0001)},
1893 static struct usb_driver sd_driver = {
1895 .id_table = device_table,
1899 .suspend = gspca_suspend,
1900 .resume = gspca_resume,
1901 .reset_resume = gspca_resume,