sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / media / video / gspca / cpia1.c
blob9ddbac680663523c46f4e8027707388695819948
1 /*
2 * cpia CPiA (1) gspca driver
4 * Copyright (C) 2010-2011 Hans de Goede <hdegoede@redhat.com>
6 * This module is adapted from the in kernel v4l1 cpia driver which is :
8 * (C) Copyright 1999-2000 Peter Pregler
9 * (C) Copyright 1999-2000 Scott J. Bertin
10 * (C) Copyright 1999-2000 Johannes Erdfelt <johannes@erdfelt.com>
11 * (C) Copyright 2000 STMicroelectronics
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #define MODULE_NAME "cpia1"
31 #include <linux/input.h>
32 #include "gspca.h"
34 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
35 MODULE_DESCRIPTION("Vision CPiA");
36 MODULE_LICENSE("GPL");
38 /* constant value's */
39 #define MAGIC_0 0x19
40 #define MAGIC_1 0x68
41 #define DATA_IN 0xc0
42 #define DATA_OUT 0x40
43 #define VIDEOSIZE_QCIF 0 /* 176x144 */
44 #define VIDEOSIZE_CIF 1 /* 352x288 */
45 #define SUBSAMPLE_420 0
46 #define SUBSAMPLE_422 1
47 #define YUVORDER_YUYV 0
48 #define YUVORDER_UYVY 1
49 #define NOT_COMPRESSED 0
50 #define COMPRESSED 1
51 #define NO_DECIMATION 0
52 #define DECIMATION_ENAB 1
53 #define EOI 0xff /* End Of Image */
54 #define EOL 0xfd /* End Of Line */
55 #define FRAME_HEADER_SIZE 64
57 /* Image grab modes */
58 #define CPIA_GRAB_SINGLE 0
59 #define CPIA_GRAB_CONTINEOUS 1
61 /* Compression parameters */
62 #define CPIA_COMPRESSION_NONE 0
63 #define CPIA_COMPRESSION_AUTO 1
64 #define CPIA_COMPRESSION_MANUAL 2
65 #define CPIA_COMPRESSION_TARGET_QUALITY 0
66 #define CPIA_COMPRESSION_TARGET_FRAMERATE 1
68 /* Return offsets for GetCameraState */
69 #define SYSTEMSTATE 0
70 #define GRABSTATE 1
71 #define STREAMSTATE 2
72 #define FATALERROR 3
73 #define CMDERROR 4
74 #define DEBUGFLAGS 5
75 #define VPSTATUS 6
76 #define ERRORCODE 7
78 /* SystemState */
79 #define UNINITIALISED_STATE 0
80 #define PASS_THROUGH_STATE 1
81 #define LO_POWER_STATE 2
82 #define HI_POWER_STATE 3
83 #define WARM_BOOT_STATE 4
85 /* GrabState */
86 #define GRAB_IDLE 0
87 #define GRAB_ACTIVE 1
88 #define GRAB_DONE 2
90 /* StreamState */
91 #define STREAM_NOT_READY 0
92 #define STREAM_READY 1
93 #define STREAM_OPEN 2
94 #define STREAM_PAUSED 3
95 #define STREAM_FINISHED 4
97 /* Fatal Error, CmdError, and DebugFlags */
98 #define CPIA_FLAG 1
99 #define SYSTEM_FLAG 2
100 #define INT_CTRL_FLAG 4
101 #define PROCESS_FLAG 8
102 #define COM_FLAG 16
103 #define VP_CTRL_FLAG 32
104 #define CAPTURE_FLAG 64
105 #define DEBUG_FLAG 128
107 /* VPStatus */
108 #define VP_STATE_OK 0x00
110 #define VP_STATE_FAILED_VIDEOINIT 0x01
111 #define VP_STATE_FAILED_AECACBINIT 0x02
112 #define VP_STATE_AEC_MAX 0x04
113 #define VP_STATE_ACB_BMAX 0x08
115 #define VP_STATE_ACB_RMIN 0x10
116 #define VP_STATE_ACB_GMIN 0x20
117 #define VP_STATE_ACB_RMAX 0x40
118 #define VP_STATE_ACB_GMAX 0x80
120 /* default (minimum) compensation values */
121 #define COMP_RED 220
122 #define COMP_GREEN1 214
123 #define COMP_GREEN2 COMP_GREEN1
124 #define COMP_BLUE 230
126 /* exposure status */
127 #define EXPOSURE_VERY_LIGHT 0
128 #define EXPOSURE_LIGHT 1
129 #define EXPOSURE_NORMAL 2
130 #define EXPOSURE_DARK 3
131 #define EXPOSURE_VERY_DARK 4
133 #define CPIA_MODULE_CPIA (0 << 5)
134 #define CPIA_MODULE_SYSTEM (1 << 5)
135 #define CPIA_MODULE_VP_CTRL (5 << 5)
136 #define CPIA_MODULE_CAPTURE (6 << 5)
137 #define CPIA_MODULE_DEBUG (7 << 5)
139 #define INPUT (DATA_IN << 8)
140 #define OUTPUT (DATA_OUT << 8)
142 #define CPIA_COMMAND_GetCPIAVersion (INPUT | CPIA_MODULE_CPIA | 1)
143 #define CPIA_COMMAND_GetPnPID (INPUT | CPIA_MODULE_CPIA | 2)
144 #define CPIA_COMMAND_GetCameraStatus (INPUT | CPIA_MODULE_CPIA | 3)
145 #define CPIA_COMMAND_GotoHiPower (OUTPUT | CPIA_MODULE_CPIA | 4)
146 #define CPIA_COMMAND_GotoLoPower (OUTPUT | CPIA_MODULE_CPIA | 5)
147 #define CPIA_COMMAND_GotoSuspend (OUTPUT | CPIA_MODULE_CPIA | 7)
148 #define CPIA_COMMAND_GotoPassThrough (OUTPUT | CPIA_MODULE_CPIA | 8)
149 #define CPIA_COMMAND_ModifyCameraStatus (OUTPUT | CPIA_MODULE_CPIA | 10)
151 #define CPIA_COMMAND_ReadVCRegs (INPUT | CPIA_MODULE_SYSTEM | 1)
152 #define CPIA_COMMAND_WriteVCReg (OUTPUT | CPIA_MODULE_SYSTEM | 2)
153 #define CPIA_COMMAND_ReadMCPorts (INPUT | CPIA_MODULE_SYSTEM | 3)
154 #define CPIA_COMMAND_WriteMCPort (OUTPUT | CPIA_MODULE_SYSTEM | 4)
155 #define CPIA_COMMAND_SetBaudRate (OUTPUT | CPIA_MODULE_SYSTEM | 5)
156 #define CPIA_COMMAND_SetECPTiming (OUTPUT | CPIA_MODULE_SYSTEM | 6)
157 #define CPIA_COMMAND_ReadIDATA (INPUT | CPIA_MODULE_SYSTEM | 7)
158 #define CPIA_COMMAND_WriteIDATA (OUTPUT | CPIA_MODULE_SYSTEM | 8)
159 #define CPIA_COMMAND_GenericCall (OUTPUT | CPIA_MODULE_SYSTEM | 9)
160 #define CPIA_COMMAND_I2CStart (OUTPUT | CPIA_MODULE_SYSTEM | 10)
161 #define CPIA_COMMAND_I2CStop (OUTPUT | CPIA_MODULE_SYSTEM | 11)
162 #define CPIA_COMMAND_I2CWrite (OUTPUT | CPIA_MODULE_SYSTEM | 12)
163 #define CPIA_COMMAND_I2CRead (INPUT | CPIA_MODULE_SYSTEM | 13)
165 #define CPIA_COMMAND_GetVPVersion (INPUT | CPIA_MODULE_VP_CTRL | 1)
166 #define CPIA_COMMAND_ResetFrameCounter (INPUT | CPIA_MODULE_VP_CTRL | 2)
167 #define CPIA_COMMAND_SetColourParams (OUTPUT | CPIA_MODULE_VP_CTRL | 3)
168 #define CPIA_COMMAND_SetExposure (OUTPUT | CPIA_MODULE_VP_CTRL | 4)
169 #define CPIA_COMMAND_SetColourBalance (OUTPUT | CPIA_MODULE_VP_CTRL | 6)
170 #define CPIA_COMMAND_SetSensorFPS (OUTPUT | CPIA_MODULE_VP_CTRL | 7)
171 #define CPIA_COMMAND_SetVPDefaults (OUTPUT | CPIA_MODULE_VP_CTRL | 8)
172 #define CPIA_COMMAND_SetApcor (OUTPUT | CPIA_MODULE_VP_CTRL | 9)
173 #define CPIA_COMMAND_SetFlickerCtrl (OUTPUT | CPIA_MODULE_VP_CTRL | 10)
174 #define CPIA_COMMAND_SetVLOffset (OUTPUT | CPIA_MODULE_VP_CTRL | 11)
175 #define CPIA_COMMAND_GetColourParams (INPUT | CPIA_MODULE_VP_CTRL | 16)
176 #define CPIA_COMMAND_GetColourBalance (INPUT | CPIA_MODULE_VP_CTRL | 17)
177 #define CPIA_COMMAND_GetExposure (INPUT | CPIA_MODULE_VP_CTRL | 18)
178 #define CPIA_COMMAND_SetSensorMatrix (OUTPUT | CPIA_MODULE_VP_CTRL | 19)
179 #define CPIA_COMMAND_ColourBars (OUTPUT | CPIA_MODULE_VP_CTRL | 25)
180 #define CPIA_COMMAND_ReadVPRegs (INPUT | CPIA_MODULE_VP_CTRL | 30)
181 #define CPIA_COMMAND_WriteVPReg (OUTPUT | CPIA_MODULE_VP_CTRL | 31)
183 #define CPIA_COMMAND_GrabFrame (OUTPUT | CPIA_MODULE_CAPTURE | 1)
184 #define CPIA_COMMAND_UploadFrame (OUTPUT | CPIA_MODULE_CAPTURE | 2)
185 #define CPIA_COMMAND_SetGrabMode (OUTPUT | CPIA_MODULE_CAPTURE | 3)
186 #define CPIA_COMMAND_InitStreamCap (OUTPUT | CPIA_MODULE_CAPTURE | 4)
187 #define CPIA_COMMAND_FiniStreamCap (OUTPUT | CPIA_MODULE_CAPTURE | 5)
188 #define CPIA_COMMAND_StartStreamCap (OUTPUT | CPIA_MODULE_CAPTURE | 6)
189 #define CPIA_COMMAND_EndStreamCap (OUTPUT | CPIA_MODULE_CAPTURE | 7)
190 #define CPIA_COMMAND_SetFormat (OUTPUT | CPIA_MODULE_CAPTURE | 8)
191 #define CPIA_COMMAND_SetROI (OUTPUT | CPIA_MODULE_CAPTURE | 9)
192 #define CPIA_COMMAND_SetCompression (OUTPUT | CPIA_MODULE_CAPTURE | 10)
193 #define CPIA_COMMAND_SetCompressionTarget (OUTPUT | CPIA_MODULE_CAPTURE | 11)
194 #define CPIA_COMMAND_SetYUVThresh (OUTPUT | CPIA_MODULE_CAPTURE | 12)
195 #define CPIA_COMMAND_SetCompressionParams (OUTPUT | CPIA_MODULE_CAPTURE | 13)
196 #define CPIA_COMMAND_DiscardFrame (OUTPUT | CPIA_MODULE_CAPTURE | 14)
197 #define CPIA_COMMAND_GrabReset (OUTPUT | CPIA_MODULE_CAPTURE | 15)
199 #define CPIA_COMMAND_OutputRS232 (OUTPUT | CPIA_MODULE_DEBUG | 1)
200 #define CPIA_COMMAND_AbortProcess (OUTPUT | CPIA_MODULE_DEBUG | 4)
201 #define CPIA_COMMAND_SetDramPage (OUTPUT | CPIA_MODULE_DEBUG | 5)
202 #define CPIA_COMMAND_StartDramUpload (OUTPUT | CPIA_MODULE_DEBUG | 6)
203 #define CPIA_COMMAND_StartDummyDtream (OUTPUT | CPIA_MODULE_DEBUG | 8)
204 #define CPIA_COMMAND_AbortStream (OUTPUT | CPIA_MODULE_DEBUG | 9)
205 #define CPIA_COMMAND_DownloadDRAM (OUTPUT | CPIA_MODULE_DEBUG | 10)
206 #define CPIA_COMMAND_Null (OUTPUT | CPIA_MODULE_DEBUG | 11)
208 #define ROUND_UP_EXP_FOR_FLICKER 15
210 /* Constants for automatic frame rate adjustment */
211 #define MAX_EXP 302
212 #define MAX_EXP_102 255
213 #define LOW_EXP 140
214 #define VERY_LOW_EXP 70
215 #define TC 94
216 #define EXP_ACC_DARK 50
217 #define EXP_ACC_LIGHT 90
218 #define HIGH_COMP_102 160
219 #define MAX_COMP 239
220 #define DARK_TIME 3
221 #define LIGHT_TIME 3
223 #define FIRMWARE_VERSION(x, y) (sd->params.version.firmwareVersion == (x) && \
224 sd->params.version.firmwareRevision == (y))
226 /* Developer's Guide Table 5 p 3-34
227 * indexed by [mains][sensorFps.baserate][sensorFps.divisor]*/
228 static u8 flicker_jumps[2][2][4] =
229 { { { 76, 38, 19, 9 }, { 92, 46, 23, 11 } },
230 { { 64, 32, 16, 8 }, { 76, 38, 19, 9} }
233 struct cam_params {
234 struct {
235 u8 firmwareVersion;
236 u8 firmwareRevision;
237 u8 vcVersion;
238 u8 vcRevision;
239 } version;
240 struct {
241 u16 vendor;
242 u16 product;
243 u16 deviceRevision;
244 } pnpID;
245 struct {
246 u8 vpVersion;
247 u8 vpRevision;
248 u16 cameraHeadID;
249 } vpVersion;
250 struct {
251 u8 systemState;
252 u8 grabState;
253 u8 streamState;
254 u8 fatalError;
255 u8 cmdError;
256 u8 debugFlags;
257 u8 vpStatus;
258 u8 errorCode;
259 } status;
260 struct {
261 u8 brightness;
262 u8 contrast;
263 u8 saturation;
264 } colourParams;
265 struct {
266 u8 gainMode;
267 u8 expMode;
268 u8 compMode;
269 u8 centreWeight;
270 u8 gain;
271 u8 fineExp;
272 u8 coarseExpLo;
273 u8 coarseExpHi;
274 u8 redComp;
275 u8 green1Comp;
276 u8 green2Comp;
277 u8 blueComp;
278 } exposure;
279 struct {
280 u8 balanceMode;
281 u8 redGain;
282 u8 greenGain;
283 u8 blueGain;
284 } colourBalance;
285 struct {
286 u8 divisor;
287 u8 baserate;
288 } sensorFps;
289 struct {
290 u8 gain1;
291 u8 gain2;
292 u8 gain4;
293 u8 gain8;
294 } apcor;
295 struct {
296 u8 disabled;
297 u8 flickerMode;
298 u8 coarseJump;
299 u8 allowableOverExposure;
300 } flickerControl;
301 struct {
302 u8 gain1;
303 u8 gain2;
304 u8 gain4;
305 u8 gain8;
306 } vlOffset;
307 struct {
308 u8 mode;
309 u8 decimation;
310 } compression;
311 struct {
312 u8 frTargeting;
313 u8 targetFR;
314 u8 targetQ;
315 } compressionTarget;
316 struct {
317 u8 yThreshold;
318 u8 uvThreshold;
319 } yuvThreshold;
320 struct {
321 u8 hysteresis;
322 u8 threshMax;
323 u8 smallStep;
324 u8 largeStep;
325 u8 decimationHysteresis;
326 u8 frDiffStepThresh;
327 u8 qDiffStepThresh;
328 u8 decimationThreshMod;
329 } compressionParams;
330 struct {
331 u8 videoSize; /* CIF/QCIF */
332 u8 subSample;
333 u8 yuvOrder;
334 } format;
335 struct { /* Intel QX3 specific data */
336 u8 qx3_detected; /* a QX3 is present */
337 u8 toplight; /* top light lit , R/W */
338 u8 bottomlight; /* bottom light lit, R/W */
339 u8 button; /* snapshot button pressed (R/O) */
340 u8 cradled; /* microscope is in cradle (R/O) */
341 } qx3;
342 struct {
343 u8 colStart; /* skip first 8*colStart pixels */
344 u8 colEnd; /* finish at 8*colEnd pixels */
345 u8 rowStart; /* skip first 4*rowStart lines */
346 u8 rowEnd; /* finish at 4*rowEnd lines */
347 } roi;
348 u8 ecpTiming;
349 u8 streamStartLine;
352 /* specific webcam descriptor */
353 struct sd {
354 struct gspca_dev gspca_dev; /* !! must be the first item */
355 struct cam_params params; /* camera settings */
357 atomic_t cam_exposure;
358 atomic_t fps;
359 int exposure_count;
360 u8 exposure_status;
361 u8 mainsFreq; /* 0 = 50hz, 1 = 60hz */
362 u8 first_frame;
363 u8 freq;
366 /* V4L2 controls supported by the driver */
367 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
368 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
369 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
370 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
371 static int sd_setsaturation(struct gspca_dev *gspca_dev, __s32 val);
372 static int sd_getsaturation(struct gspca_dev *gspca_dev, __s32 *val);
373 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
374 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
375 static int sd_setcomptarget(struct gspca_dev *gspca_dev, __s32 val);
376 static int sd_getcomptarget(struct gspca_dev *gspca_dev, __s32 *val);
377 static int sd_setilluminator1(struct gspca_dev *gspca_dev, __s32 val);
378 static int sd_getilluminator1(struct gspca_dev *gspca_dev, __s32 *val);
379 static int sd_setilluminator2(struct gspca_dev *gspca_dev, __s32 val);
380 static int sd_getilluminator2(struct gspca_dev *gspca_dev, __s32 *val);
382 static const struct ctrl sd_ctrls[] = {
384 #define BRIGHTNESS_IDX 0
386 .id = V4L2_CID_BRIGHTNESS,
387 .type = V4L2_CTRL_TYPE_INTEGER,
388 .name = "Brightness",
389 .minimum = 0,
390 .maximum = 100,
391 .step = 1,
392 #define BRIGHTNESS_DEF 50
393 .default_value = BRIGHTNESS_DEF,
394 .flags = 0,
396 .set = sd_setbrightness,
397 .get = sd_getbrightness,
399 #define CONTRAST_IDX 1
402 .id = V4L2_CID_CONTRAST,
403 .type = V4L2_CTRL_TYPE_INTEGER,
404 .name = "Contrast",
405 .minimum = 0,
406 .maximum = 96,
407 .step = 8,
408 #define CONTRAST_DEF 48
409 .default_value = CONTRAST_DEF,
411 .set = sd_setcontrast,
412 .get = sd_getcontrast,
414 #define SATURATION_IDX 2
417 .id = V4L2_CID_SATURATION,
418 .type = V4L2_CTRL_TYPE_INTEGER,
419 .name = "Saturation",
420 .minimum = 0,
421 .maximum = 100,
422 .step = 1,
423 #define SATURATION_DEF 50
424 .default_value = SATURATION_DEF,
426 .set = sd_setsaturation,
427 .get = sd_getsaturation,
429 #define POWER_LINE_FREQUENCY_IDX 3
432 .id = V4L2_CID_POWER_LINE_FREQUENCY,
433 .type = V4L2_CTRL_TYPE_MENU,
434 .name = "Light frequency filter",
435 .minimum = 0,
436 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
437 .step = 1,
438 #define FREQ_DEF 1
439 .default_value = FREQ_DEF,
441 .set = sd_setfreq,
442 .get = sd_getfreq,
444 #define ILLUMINATORS_1_IDX 4
447 .id = V4L2_CID_ILLUMINATORS_1,
448 .type = V4L2_CTRL_TYPE_BOOLEAN,
449 .name = "Illuminator 1",
450 .minimum = 0,
451 .maximum = 1,
452 .step = 1,
453 #define ILLUMINATORS_1_DEF 0
454 .default_value = ILLUMINATORS_1_DEF,
456 .set = sd_setilluminator1,
457 .get = sd_getilluminator1,
459 #define ILLUMINATORS_2_IDX 5
462 .id = V4L2_CID_ILLUMINATORS_2,
463 .type = V4L2_CTRL_TYPE_BOOLEAN,
464 .name = "Illuminator 2",
465 .minimum = 0,
466 .maximum = 1,
467 .step = 1,
468 #define ILLUMINATORS_2_DEF 0
469 .default_value = ILLUMINATORS_2_DEF,
471 .set = sd_setilluminator2,
472 .get = sd_getilluminator2,
474 #define COMP_TARGET_IDX 6
477 #define V4L2_CID_COMP_TARGET V4L2_CID_PRIVATE_BASE
478 .id = V4L2_CID_COMP_TARGET,
479 .type = V4L2_CTRL_TYPE_MENU,
480 .name = "Compression Target",
481 .minimum = 0,
482 .maximum = 1,
483 .step = 1,
484 #define COMP_TARGET_DEF CPIA_COMPRESSION_TARGET_QUALITY
485 .default_value = COMP_TARGET_DEF,
487 .set = sd_setcomptarget,
488 .get = sd_getcomptarget,
492 static const struct v4l2_pix_format mode[] = {
493 {160, 120, V4L2_PIX_FMT_CPIA1, V4L2_FIELD_NONE,
494 /* The sizeimage is trial and error, as with low framerates
495 the camera will pad out usb frames, making the image
496 data larger then strictly necessary */
497 .bytesperline = 160,
498 .sizeimage = 65536,
499 .colorspace = V4L2_COLORSPACE_SRGB,
500 .priv = 3},
501 {176, 144, V4L2_PIX_FMT_CPIA1, V4L2_FIELD_NONE,
502 .bytesperline = 172,
503 .sizeimage = 65536,
504 .colorspace = V4L2_COLORSPACE_SRGB,
505 .priv = 2},
506 {320, 240, V4L2_PIX_FMT_CPIA1, V4L2_FIELD_NONE,
507 .bytesperline = 320,
508 .sizeimage = 262144,
509 .colorspace = V4L2_COLORSPACE_SRGB,
510 .priv = 1},
511 {352, 288, V4L2_PIX_FMT_CPIA1, V4L2_FIELD_NONE,
512 .bytesperline = 352,
513 .sizeimage = 262144,
514 .colorspace = V4L2_COLORSPACE_SRGB,
515 .priv = 0},
518 /**********************************************************************
520 * General functions
522 **********************************************************************/
524 static int cpia_usb_transferCmd(struct gspca_dev *gspca_dev, u8 *command)
526 u8 requesttype;
527 unsigned int pipe;
528 int ret, databytes = command[6] | (command[7] << 8);
529 /* Sometimes we see spurious EPIPE errors */
530 int retries = 3;
532 if (command[0] == DATA_IN) {
533 pipe = usb_rcvctrlpipe(gspca_dev->dev, 0);
534 requesttype = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
535 } else if (command[0] == DATA_OUT) {
536 pipe = usb_sndctrlpipe(gspca_dev->dev, 0);
537 requesttype = USB_TYPE_VENDOR | USB_RECIP_DEVICE;
538 } else {
539 PDEBUG(D_ERR, "Unexpected first byte of command: %x",
540 command[0]);
541 return -EINVAL;
544 retry:
545 ret = usb_control_msg(gspca_dev->dev, pipe,
546 command[1],
547 requesttype,
548 command[2] | (command[3] << 8),
549 command[4] | (command[5] << 8),
550 gspca_dev->usb_buf, databytes, 1000);
552 if (ret < 0)
553 err("usb_control_msg %02x, error %d", command[1],
554 ret);
556 if (ret == -EPIPE && retries > 0) {
557 retries--;
558 goto retry;
561 return (ret < 0) ? ret : 0;
564 /* send an arbitrary command to the camera */
565 static int do_command(struct gspca_dev *gspca_dev, u16 command,
566 u8 a, u8 b, u8 c, u8 d)
568 struct sd *sd = (struct sd *) gspca_dev;
569 int ret, datasize;
570 u8 cmd[8];
572 switch (command) {
573 case CPIA_COMMAND_GetCPIAVersion:
574 case CPIA_COMMAND_GetPnPID:
575 case CPIA_COMMAND_GetCameraStatus:
576 case CPIA_COMMAND_GetVPVersion:
577 case CPIA_COMMAND_GetColourParams:
578 case CPIA_COMMAND_GetColourBalance:
579 case CPIA_COMMAND_GetExposure:
580 datasize = 8;
581 break;
582 case CPIA_COMMAND_ReadMCPorts:
583 case CPIA_COMMAND_ReadVCRegs:
584 datasize = 4;
585 break;
586 default:
587 datasize = 0;
588 break;
591 cmd[0] = command >> 8;
592 cmd[1] = command & 0xff;
593 cmd[2] = a;
594 cmd[3] = b;
595 cmd[4] = c;
596 cmd[5] = d;
597 cmd[6] = datasize;
598 cmd[7] = 0;
600 ret = cpia_usb_transferCmd(gspca_dev, cmd);
601 if (ret)
602 return ret;
604 switch (command) {
605 case CPIA_COMMAND_GetCPIAVersion:
606 sd->params.version.firmwareVersion = gspca_dev->usb_buf[0];
607 sd->params.version.firmwareRevision = gspca_dev->usb_buf[1];
608 sd->params.version.vcVersion = gspca_dev->usb_buf[2];
609 sd->params.version.vcRevision = gspca_dev->usb_buf[3];
610 break;
611 case CPIA_COMMAND_GetPnPID:
612 sd->params.pnpID.vendor =
613 gspca_dev->usb_buf[0] | (gspca_dev->usb_buf[1] << 8);
614 sd->params.pnpID.product =
615 gspca_dev->usb_buf[2] | (gspca_dev->usb_buf[3] << 8);
616 sd->params.pnpID.deviceRevision =
617 gspca_dev->usb_buf[4] | (gspca_dev->usb_buf[5] << 8);
618 break;
619 case CPIA_COMMAND_GetCameraStatus:
620 sd->params.status.systemState = gspca_dev->usb_buf[0];
621 sd->params.status.grabState = gspca_dev->usb_buf[1];
622 sd->params.status.streamState = gspca_dev->usb_buf[2];
623 sd->params.status.fatalError = gspca_dev->usb_buf[3];
624 sd->params.status.cmdError = gspca_dev->usb_buf[4];
625 sd->params.status.debugFlags = gspca_dev->usb_buf[5];
626 sd->params.status.vpStatus = gspca_dev->usb_buf[6];
627 sd->params.status.errorCode = gspca_dev->usb_buf[7];
628 break;
629 case CPIA_COMMAND_GetVPVersion:
630 sd->params.vpVersion.vpVersion = gspca_dev->usb_buf[0];
631 sd->params.vpVersion.vpRevision = gspca_dev->usb_buf[1];
632 sd->params.vpVersion.cameraHeadID =
633 gspca_dev->usb_buf[2] | (gspca_dev->usb_buf[3] << 8);
634 break;
635 case CPIA_COMMAND_GetColourParams:
636 sd->params.colourParams.brightness = gspca_dev->usb_buf[0];
637 sd->params.colourParams.contrast = gspca_dev->usb_buf[1];
638 sd->params.colourParams.saturation = gspca_dev->usb_buf[2];
639 break;
640 case CPIA_COMMAND_GetColourBalance:
641 sd->params.colourBalance.redGain = gspca_dev->usb_buf[0];
642 sd->params.colourBalance.greenGain = gspca_dev->usb_buf[1];
643 sd->params.colourBalance.blueGain = gspca_dev->usb_buf[2];
644 break;
645 case CPIA_COMMAND_GetExposure:
646 sd->params.exposure.gain = gspca_dev->usb_buf[0];
647 sd->params.exposure.fineExp = gspca_dev->usb_buf[1];
648 sd->params.exposure.coarseExpLo = gspca_dev->usb_buf[2];
649 sd->params.exposure.coarseExpHi = gspca_dev->usb_buf[3];
650 sd->params.exposure.redComp = gspca_dev->usb_buf[4];
651 sd->params.exposure.green1Comp = gspca_dev->usb_buf[5];
652 sd->params.exposure.green2Comp = gspca_dev->usb_buf[6];
653 sd->params.exposure.blueComp = gspca_dev->usb_buf[7];
654 break;
656 case CPIA_COMMAND_ReadMCPorts:
657 /* test button press */
658 a = ((gspca_dev->usb_buf[1] & 0x02) == 0);
659 if (a != sd->params.qx3.button) {
660 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
661 input_report_key(gspca_dev->input_dev, KEY_CAMERA, a);
662 input_sync(gspca_dev->input_dev);
663 #endif
664 sd->params.qx3.button = a;
666 if (sd->params.qx3.button) {
667 /* button pressed - unlock the latch */
668 do_command(gspca_dev, CPIA_COMMAND_WriteMCPort,
669 3, 0xdf, 0xdf, 0);
670 do_command(gspca_dev, CPIA_COMMAND_WriteMCPort,
671 3, 0xff, 0xff, 0);
674 /* test whether microscope is cradled */
675 sd->params.qx3.cradled = ((gspca_dev->usb_buf[2] & 0x40) == 0);
676 break;
679 return 0;
682 /* send a command to the camera with an additional data transaction */
683 static int do_command_extended(struct gspca_dev *gspca_dev, u16 command,
684 u8 a, u8 b, u8 c, u8 d,
685 u8 e, u8 f, u8 g, u8 h,
686 u8 i, u8 j, u8 k, u8 l)
688 u8 cmd[8];
690 cmd[0] = command >> 8;
691 cmd[1] = command & 0xff;
692 cmd[2] = a;
693 cmd[3] = b;
694 cmd[4] = c;
695 cmd[5] = d;
696 cmd[6] = 8;
697 cmd[7] = 0;
698 gspca_dev->usb_buf[0] = e;
699 gspca_dev->usb_buf[1] = f;
700 gspca_dev->usb_buf[2] = g;
701 gspca_dev->usb_buf[3] = h;
702 gspca_dev->usb_buf[4] = i;
703 gspca_dev->usb_buf[5] = j;
704 gspca_dev->usb_buf[6] = k;
705 gspca_dev->usb_buf[7] = l;
707 return cpia_usb_transferCmd(gspca_dev, cmd);
710 /* find_over_exposure
711 * Finds a suitable value of OverExposure for use with SetFlickerCtrl
712 * Some calculation is required because this value changes with the brightness
713 * set with SetColourParameters
715 * Parameters: Brightness - last brightness value set with SetColourParameters
717 * Returns: OverExposure value to use with SetFlickerCtrl
719 #define FLICKER_MAX_EXPOSURE 250
720 #define FLICKER_ALLOWABLE_OVER_EXPOSURE 146
721 #define FLICKER_BRIGHTNESS_CONSTANT 59
722 static int find_over_exposure(int brightness)
724 int MaxAllowableOverExposure, OverExposure;
726 MaxAllowableOverExposure = FLICKER_MAX_EXPOSURE - brightness -
727 FLICKER_BRIGHTNESS_CONSTANT;
729 if (MaxAllowableOverExposure < FLICKER_ALLOWABLE_OVER_EXPOSURE)
730 OverExposure = MaxAllowableOverExposure;
731 else
732 OverExposure = FLICKER_ALLOWABLE_OVER_EXPOSURE;
734 return OverExposure;
736 #undef FLICKER_MAX_EXPOSURE
737 #undef FLICKER_ALLOWABLE_OVER_EXPOSURE
738 #undef FLICKER_BRIGHTNESS_CONSTANT
740 /* initialise cam_data structure */
741 static void reset_camera_params(struct gspca_dev *gspca_dev)
743 struct sd *sd = (struct sd *) gspca_dev;
744 struct cam_params *params = &sd->params;
746 /* The following parameter values are the defaults from
747 * "Software Developer's Guide for CPiA Cameras". Any changes
748 * to the defaults are noted in comments. */
749 params->colourParams.brightness = BRIGHTNESS_DEF;
750 params->colourParams.contrast = CONTRAST_DEF;
751 params->colourParams.saturation = SATURATION_DEF;
752 params->exposure.gainMode = 4;
753 params->exposure.expMode = 2; /* AEC */
754 params->exposure.compMode = 1;
755 params->exposure.centreWeight = 1;
756 params->exposure.gain = 0;
757 params->exposure.fineExp = 0;
758 params->exposure.coarseExpLo = 185;
759 params->exposure.coarseExpHi = 0;
760 params->exposure.redComp = COMP_RED;
761 params->exposure.green1Comp = COMP_GREEN1;
762 params->exposure.green2Comp = COMP_GREEN2;
763 params->exposure.blueComp = COMP_BLUE;
764 params->colourBalance.balanceMode = 2; /* ACB */
765 params->colourBalance.redGain = 32;
766 params->colourBalance.greenGain = 6;
767 params->colourBalance.blueGain = 92;
768 params->apcor.gain1 = 0x18;
769 params->apcor.gain2 = 0x16;
770 params->apcor.gain4 = 0x24;
771 params->apcor.gain8 = 0x34;
772 params->flickerControl.flickerMode = 0;
773 params->flickerControl.disabled = 1;
775 params->flickerControl.coarseJump =
776 flicker_jumps[sd->mainsFreq]
777 [params->sensorFps.baserate]
778 [params->sensorFps.divisor];
779 params->flickerControl.allowableOverExposure =
780 find_over_exposure(params->colourParams.brightness);
781 params->vlOffset.gain1 = 20;
782 params->vlOffset.gain2 = 24;
783 params->vlOffset.gain4 = 26;
784 params->vlOffset.gain8 = 26;
785 params->compressionParams.hysteresis = 3;
786 params->compressionParams.threshMax = 11;
787 params->compressionParams.smallStep = 1;
788 params->compressionParams.largeStep = 3;
789 params->compressionParams.decimationHysteresis = 2;
790 params->compressionParams.frDiffStepThresh = 5;
791 params->compressionParams.qDiffStepThresh = 3;
792 params->compressionParams.decimationThreshMod = 2;
793 /* End of default values from Software Developer's Guide */
795 /* Set Sensor FPS to 15fps. This seems better than 30fps
796 * for indoor lighting. */
797 params->sensorFps.divisor = 1;
798 params->sensorFps.baserate = 1;
800 params->yuvThreshold.yThreshold = 6; /* From windows driver */
801 params->yuvThreshold.uvThreshold = 6; /* From windows driver */
803 params->format.subSample = SUBSAMPLE_420;
804 params->format.yuvOrder = YUVORDER_YUYV;
806 params->compression.mode = CPIA_COMPRESSION_AUTO;
807 params->compression.decimation = NO_DECIMATION;
809 params->compressionTarget.frTargeting = COMP_TARGET_DEF;
810 params->compressionTarget.targetFR = 15; /* From windows driver */
811 params->compressionTarget.targetQ = 5; /* From windows driver */
813 params->qx3.qx3_detected = 0;
814 params->qx3.toplight = 0;
815 params->qx3.bottomlight = 0;
816 params->qx3.button = 0;
817 params->qx3.cradled = 0;
820 static void printstatus(struct cam_params *params)
822 PDEBUG(D_PROBE, "status: %02x %02x %02x %02x %02x %02x %02x %02x",
823 params->status.systemState, params->status.grabState,
824 params->status.streamState, params->status.fatalError,
825 params->status.cmdError, params->status.debugFlags,
826 params->status.vpStatus, params->status.errorCode);
829 static int goto_low_power(struct gspca_dev *gspca_dev)
831 struct sd *sd = (struct sd *) gspca_dev;
832 int ret;
834 ret = do_command(gspca_dev, CPIA_COMMAND_GotoLoPower, 0, 0, 0, 0);
835 if (ret)
836 return ret;
838 ret = do_command(gspca_dev, CPIA_COMMAND_GetCameraStatus, 0, 0, 0, 0);
839 if (ret)
840 return ret;
842 if (sd->params.status.systemState != LO_POWER_STATE) {
843 if (sd->params.status.systemState != WARM_BOOT_STATE) {
844 PDEBUG(D_ERR,
845 "unexpected state after lo power cmd: %02x",
846 sd->params.status.systemState);
847 printstatus(&sd->params);
849 return -EIO;
852 PDEBUG(D_CONF, "camera now in LOW power state");
853 return 0;
856 static int goto_high_power(struct gspca_dev *gspca_dev)
858 struct sd *sd = (struct sd *) gspca_dev;
859 int ret;
861 ret = do_command(gspca_dev, CPIA_COMMAND_GotoHiPower, 0, 0, 0, 0);
862 if (ret)
863 return ret;
865 msleep_interruptible(40); /* windows driver does it too */
867 if (signal_pending(current))
868 return -EINTR;
870 do_command(gspca_dev, CPIA_COMMAND_GetCameraStatus, 0, 0, 0, 0);
871 if (ret)
872 return ret;
874 if (sd->params.status.systemState != HI_POWER_STATE) {
875 PDEBUG(D_ERR, "unexpected state after hi power cmd: %02x",
876 sd->params.status.systemState);
877 printstatus(&sd->params);
878 return -EIO;
881 PDEBUG(D_CONF, "camera now in HIGH power state");
882 return 0;
885 static int get_version_information(struct gspca_dev *gspca_dev)
887 int ret;
889 /* GetCPIAVersion */
890 ret = do_command(gspca_dev, CPIA_COMMAND_GetCPIAVersion, 0, 0, 0, 0);
891 if (ret)
892 return ret;
894 /* GetPnPID */
895 return do_command(gspca_dev, CPIA_COMMAND_GetPnPID, 0, 0, 0, 0);
898 static int save_camera_state(struct gspca_dev *gspca_dev)
900 int ret;
902 ret = do_command(gspca_dev, CPIA_COMMAND_GetColourBalance, 0, 0, 0, 0);
903 if (ret)
904 return ret;
906 return do_command(gspca_dev, CPIA_COMMAND_GetExposure, 0, 0, 0, 0);
909 static int command_setformat(struct gspca_dev *gspca_dev)
911 struct sd *sd = (struct sd *) gspca_dev;
912 int ret;
914 ret = do_command(gspca_dev, CPIA_COMMAND_SetFormat,
915 sd->params.format.videoSize,
916 sd->params.format.subSample,
917 sd->params.format.yuvOrder, 0);
918 if (ret)
919 return ret;
921 return do_command(gspca_dev, CPIA_COMMAND_SetROI,
922 sd->params.roi.colStart, sd->params.roi.colEnd,
923 sd->params.roi.rowStart, sd->params.roi.rowEnd);
926 static int command_setcolourparams(struct gspca_dev *gspca_dev)
928 struct sd *sd = (struct sd *) gspca_dev;
929 return do_command(gspca_dev, CPIA_COMMAND_SetColourParams,
930 sd->params.colourParams.brightness,
931 sd->params.colourParams.contrast,
932 sd->params.colourParams.saturation, 0);
935 static int command_setapcor(struct gspca_dev *gspca_dev)
937 struct sd *sd = (struct sd *) gspca_dev;
938 return do_command(gspca_dev, CPIA_COMMAND_SetApcor,
939 sd->params.apcor.gain1,
940 sd->params.apcor.gain2,
941 sd->params.apcor.gain4,
942 sd->params.apcor.gain8);
945 static int command_setvloffset(struct gspca_dev *gspca_dev)
947 struct sd *sd = (struct sd *) gspca_dev;
948 return do_command(gspca_dev, CPIA_COMMAND_SetVLOffset,
949 sd->params.vlOffset.gain1,
950 sd->params.vlOffset.gain2,
951 sd->params.vlOffset.gain4,
952 sd->params.vlOffset.gain8);
955 static int command_setexposure(struct gspca_dev *gspca_dev)
957 struct sd *sd = (struct sd *) gspca_dev;
958 int ret;
960 ret = do_command_extended(gspca_dev, CPIA_COMMAND_SetExposure,
961 sd->params.exposure.gainMode,
963 sd->params.exposure.compMode,
964 sd->params.exposure.centreWeight,
965 sd->params.exposure.gain,
966 sd->params.exposure.fineExp,
967 sd->params.exposure.coarseExpLo,
968 sd->params.exposure.coarseExpHi,
969 sd->params.exposure.redComp,
970 sd->params.exposure.green1Comp,
971 sd->params.exposure.green2Comp,
972 sd->params.exposure.blueComp);
973 if (ret)
974 return ret;
976 if (sd->params.exposure.expMode != 1) {
977 ret = do_command_extended(gspca_dev, CPIA_COMMAND_SetExposure,
979 sd->params.exposure.expMode,
980 0, 0,
981 sd->params.exposure.gain,
982 sd->params.exposure.fineExp,
983 sd->params.exposure.coarseExpLo,
984 sd->params.exposure.coarseExpHi,
985 0, 0, 0, 0);
988 return ret;
991 static int command_setcolourbalance(struct gspca_dev *gspca_dev)
993 struct sd *sd = (struct sd *) gspca_dev;
995 if (sd->params.colourBalance.balanceMode == 1) {
996 int ret;
998 ret = do_command(gspca_dev, CPIA_COMMAND_SetColourBalance,
1000 sd->params.colourBalance.redGain,
1001 sd->params.colourBalance.greenGain,
1002 sd->params.colourBalance.blueGain);
1003 if (ret)
1004 return ret;
1006 return do_command(gspca_dev, CPIA_COMMAND_SetColourBalance,
1007 3, 0, 0, 0);
1009 if (sd->params.colourBalance.balanceMode == 2) {
1010 return do_command(gspca_dev, CPIA_COMMAND_SetColourBalance,
1011 2, 0, 0, 0);
1013 if (sd->params.colourBalance.balanceMode == 3) {
1014 return do_command(gspca_dev, CPIA_COMMAND_SetColourBalance,
1015 3, 0, 0, 0);
1018 return -EINVAL;
1021 static int command_setcompressiontarget(struct gspca_dev *gspca_dev)
1023 struct sd *sd = (struct sd *) gspca_dev;
1025 return do_command(gspca_dev, CPIA_COMMAND_SetCompressionTarget,
1026 sd->params.compressionTarget.frTargeting,
1027 sd->params.compressionTarget.targetFR,
1028 sd->params.compressionTarget.targetQ, 0);
1031 static int command_setyuvtresh(struct gspca_dev *gspca_dev)
1033 struct sd *sd = (struct sd *) gspca_dev;
1035 return do_command(gspca_dev, CPIA_COMMAND_SetYUVThresh,
1036 sd->params.yuvThreshold.yThreshold,
1037 sd->params.yuvThreshold.uvThreshold, 0, 0);
1040 static int command_setcompressionparams(struct gspca_dev *gspca_dev)
1042 struct sd *sd = (struct sd *) gspca_dev;
1044 return do_command_extended(gspca_dev,
1045 CPIA_COMMAND_SetCompressionParams,
1046 0, 0, 0, 0,
1047 sd->params.compressionParams.hysteresis,
1048 sd->params.compressionParams.threshMax,
1049 sd->params.compressionParams.smallStep,
1050 sd->params.compressionParams.largeStep,
1051 sd->params.compressionParams.decimationHysteresis,
1052 sd->params.compressionParams.frDiffStepThresh,
1053 sd->params.compressionParams.qDiffStepThresh,
1054 sd->params.compressionParams.decimationThreshMod);
1057 static int command_setcompression(struct gspca_dev *gspca_dev)
1059 struct sd *sd = (struct sd *) gspca_dev;
1061 return do_command(gspca_dev, CPIA_COMMAND_SetCompression,
1062 sd->params.compression.mode,
1063 sd->params.compression.decimation, 0, 0);
1066 static int command_setsensorfps(struct gspca_dev *gspca_dev)
1068 struct sd *sd = (struct sd *) gspca_dev;
1070 return do_command(gspca_dev, CPIA_COMMAND_SetSensorFPS,
1071 sd->params.sensorFps.divisor,
1072 sd->params.sensorFps.baserate, 0, 0);
1075 static int command_setflickerctrl(struct gspca_dev *gspca_dev)
1077 struct sd *sd = (struct sd *) gspca_dev;
1079 return do_command(gspca_dev, CPIA_COMMAND_SetFlickerCtrl,
1080 sd->params.flickerControl.flickerMode,
1081 sd->params.flickerControl.coarseJump,
1082 sd->params.flickerControl.allowableOverExposure,
1086 static int command_setecptiming(struct gspca_dev *gspca_dev)
1088 struct sd *sd = (struct sd *) gspca_dev;
1090 return do_command(gspca_dev, CPIA_COMMAND_SetECPTiming,
1091 sd->params.ecpTiming, 0, 0, 0);
1094 static int command_pause(struct gspca_dev *gspca_dev)
1096 return do_command(gspca_dev, CPIA_COMMAND_EndStreamCap, 0, 0, 0, 0);
1099 static int command_resume(struct gspca_dev *gspca_dev)
1101 struct sd *sd = (struct sd *) gspca_dev;
1103 return do_command(gspca_dev, CPIA_COMMAND_InitStreamCap,
1104 0, sd->params.streamStartLine, 0, 0);
1107 static int command_setlights(struct gspca_dev *gspca_dev)
1109 struct sd *sd = (struct sd *) gspca_dev;
1110 int ret, p1, p2;
1112 if (!sd->params.qx3.qx3_detected)
1113 return 0;
1115 p1 = (sd->params.qx3.bottomlight == 0) << 1;
1116 p2 = (sd->params.qx3.toplight == 0) << 3;
1118 ret = do_command(gspca_dev, CPIA_COMMAND_WriteVCReg,
1119 0x90, 0x8f, 0x50, 0);
1120 if (ret)
1121 return ret;
1123 return do_command(gspca_dev, CPIA_COMMAND_WriteMCPort, 2, 0,
1124 p1 | p2 | 0xe0, 0);
1127 static int set_flicker(struct gspca_dev *gspca_dev, int on, int apply)
1129 /* Everything in here is from the Windows driver */
1130 /* define for compgain calculation */
1131 #if 0
1132 #define COMPGAIN(base, curexp, newexp) \
1133 (u8) ((((float) base - 128.0) * ((float) curexp / (float) newexp)) + 128.5)
1134 #define EXP_FROM_COMP(basecomp, curcomp, curexp) \
1135 (u16)((float)curexp * (float)(u8)(curcomp + 128) / \
1136 (float)(u8)(basecomp - 128))
1137 #else
1138 /* equivalent functions without floating point math */
1139 #define COMPGAIN(base, curexp, newexp) \
1140 (u8)(128 + (((u32)(2*(base-128)*curexp + newexp)) / (2 * newexp)))
1141 #define EXP_FROM_COMP(basecomp, curcomp, curexp) \
1142 (u16)(((u32)(curexp * (u8)(curcomp + 128)) / (u8)(basecomp - 128)))
1143 #endif
1145 struct sd *sd = (struct sd *) gspca_dev;
1146 int currentexp = sd->params.exposure.coarseExpLo +
1147 sd->params.exposure.coarseExpHi * 256;
1148 int ret, startexp;
1150 if (on) {
1151 int cj = sd->params.flickerControl.coarseJump;
1152 sd->params.flickerControl.flickerMode = 1;
1153 sd->params.flickerControl.disabled = 0;
1154 if (sd->params.exposure.expMode != 2) {
1155 sd->params.exposure.expMode = 2;
1156 sd->exposure_status = EXPOSURE_NORMAL;
1158 currentexp = currentexp << sd->params.exposure.gain;
1159 sd->params.exposure.gain = 0;
1160 /* round down current exposure to nearest value */
1161 startexp = (currentexp + ROUND_UP_EXP_FOR_FLICKER) / cj;
1162 if (startexp < 1)
1163 startexp = 1;
1164 startexp = (startexp * cj) - 1;
1165 if (FIRMWARE_VERSION(1, 2))
1166 while (startexp > MAX_EXP_102)
1167 startexp -= cj;
1168 else
1169 while (startexp > MAX_EXP)
1170 startexp -= cj;
1171 sd->params.exposure.coarseExpLo = startexp & 0xff;
1172 sd->params.exposure.coarseExpHi = startexp >> 8;
1173 if (currentexp > startexp) {
1174 if (currentexp > (2 * startexp))
1175 currentexp = 2 * startexp;
1176 sd->params.exposure.redComp =
1177 COMPGAIN(COMP_RED, currentexp, startexp);
1178 sd->params.exposure.green1Comp =
1179 COMPGAIN(COMP_GREEN1, currentexp, startexp);
1180 sd->params.exposure.green2Comp =
1181 COMPGAIN(COMP_GREEN2, currentexp, startexp);
1182 sd->params.exposure.blueComp =
1183 COMPGAIN(COMP_BLUE, currentexp, startexp);
1184 } else {
1185 sd->params.exposure.redComp = COMP_RED;
1186 sd->params.exposure.green1Comp = COMP_GREEN1;
1187 sd->params.exposure.green2Comp = COMP_GREEN2;
1188 sd->params.exposure.blueComp = COMP_BLUE;
1190 if (FIRMWARE_VERSION(1, 2))
1191 sd->params.exposure.compMode = 0;
1192 else
1193 sd->params.exposure.compMode = 1;
1195 sd->params.apcor.gain1 = 0x18;
1196 sd->params.apcor.gain2 = 0x18;
1197 sd->params.apcor.gain4 = 0x16;
1198 sd->params.apcor.gain8 = 0x14;
1199 } else {
1200 sd->params.flickerControl.flickerMode = 0;
1201 sd->params.flickerControl.disabled = 1;
1202 /* Average equivalent coarse for each comp channel */
1203 startexp = EXP_FROM_COMP(COMP_RED,
1204 sd->params.exposure.redComp, currentexp);
1205 startexp += EXP_FROM_COMP(COMP_GREEN1,
1206 sd->params.exposure.green1Comp, currentexp);
1207 startexp += EXP_FROM_COMP(COMP_GREEN2,
1208 sd->params.exposure.green2Comp, currentexp);
1209 startexp += EXP_FROM_COMP(COMP_BLUE,
1210 sd->params.exposure.blueComp, currentexp);
1211 startexp = startexp >> 2;
1212 while (startexp > MAX_EXP && sd->params.exposure.gain <
1213 sd->params.exposure.gainMode - 1) {
1214 startexp = startexp >> 1;
1215 ++sd->params.exposure.gain;
1217 if (FIRMWARE_VERSION(1, 2) && startexp > MAX_EXP_102)
1218 startexp = MAX_EXP_102;
1219 if (startexp > MAX_EXP)
1220 startexp = MAX_EXP;
1221 sd->params.exposure.coarseExpLo = startexp & 0xff;
1222 sd->params.exposure.coarseExpHi = startexp >> 8;
1223 sd->params.exposure.redComp = COMP_RED;
1224 sd->params.exposure.green1Comp = COMP_GREEN1;
1225 sd->params.exposure.green2Comp = COMP_GREEN2;
1226 sd->params.exposure.blueComp = COMP_BLUE;
1227 sd->params.exposure.compMode = 1;
1228 sd->params.apcor.gain1 = 0x18;
1229 sd->params.apcor.gain2 = 0x16;
1230 sd->params.apcor.gain4 = 0x24;
1231 sd->params.apcor.gain8 = 0x34;
1233 sd->params.vlOffset.gain1 = 20;
1234 sd->params.vlOffset.gain2 = 24;
1235 sd->params.vlOffset.gain4 = 26;
1236 sd->params.vlOffset.gain8 = 26;
1238 if (apply) {
1239 ret = command_setexposure(gspca_dev);
1240 if (ret)
1241 return ret;
1243 ret = command_setapcor(gspca_dev);
1244 if (ret)
1245 return ret;
1247 ret = command_setvloffset(gspca_dev);
1248 if (ret)
1249 return ret;
1251 ret = command_setflickerctrl(gspca_dev);
1252 if (ret)
1253 return ret;
1256 return 0;
1257 #undef EXP_FROM_COMP
1258 #undef COMPGAIN
1261 /* monitor the exposure and adjust the sensor frame rate if needed */
1262 static void monitor_exposure(struct gspca_dev *gspca_dev)
1264 struct sd *sd = (struct sd *) gspca_dev;
1265 u8 exp_acc, bcomp, gain, coarseL, cmd[8];
1266 int ret, light_exp, dark_exp, very_dark_exp;
1267 int old_exposure, new_exposure, framerate;
1268 int setfps = 0, setexp = 0, setflicker = 0;
1270 /* get necessary stats and register settings from camera */
1271 /* do_command can't handle this, so do it ourselves */
1272 cmd[0] = CPIA_COMMAND_ReadVPRegs >> 8;
1273 cmd[1] = CPIA_COMMAND_ReadVPRegs & 0xff;
1274 cmd[2] = 30;
1275 cmd[3] = 4;
1276 cmd[4] = 9;
1277 cmd[5] = 8;
1278 cmd[6] = 8;
1279 cmd[7] = 0;
1280 ret = cpia_usb_transferCmd(gspca_dev, cmd);
1281 if (ret) {
1282 err("ReadVPRegs(30,4,9,8) - failed: %d", ret);
1283 return;
1285 exp_acc = gspca_dev->usb_buf[0];
1286 bcomp = gspca_dev->usb_buf[1];
1287 gain = gspca_dev->usb_buf[2];
1288 coarseL = gspca_dev->usb_buf[3];
1290 light_exp = sd->params.colourParams.brightness +
1291 TC - 50 + EXP_ACC_LIGHT;
1292 if (light_exp > 255)
1293 light_exp = 255;
1294 dark_exp = sd->params.colourParams.brightness +
1295 TC - 50 - EXP_ACC_DARK;
1296 if (dark_exp < 0)
1297 dark_exp = 0;
1298 very_dark_exp = dark_exp / 2;
1300 old_exposure = sd->params.exposure.coarseExpHi * 256 +
1301 sd->params.exposure.coarseExpLo;
1303 if (!sd->params.flickerControl.disabled) {
1304 /* Flicker control on */
1305 int max_comp = FIRMWARE_VERSION(1, 2) ? MAX_COMP :
1306 HIGH_COMP_102;
1307 bcomp += 128; /* decode */
1308 if (bcomp >= max_comp && exp_acc < dark_exp) {
1309 /* dark */
1310 if (exp_acc < very_dark_exp) {
1311 /* very dark */
1312 if (sd->exposure_status == EXPOSURE_VERY_DARK)
1313 ++sd->exposure_count;
1314 else {
1315 sd->exposure_status =
1316 EXPOSURE_VERY_DARK;
1317 sd->exposure_count = 1;
1319 } else {
1320 /* just dark */
1321 if (sd->exposure_status == EXPOSURE_DARK)
1322 ++sd->exposure_count;
1323 else {
1324 sd->exposure_status = EXPOSURE_DARK;
1325 sd->exposure_count = 1;
1328 } else if (old_exposure <= LOW_EXP || exp_acc > light_exp) {
1329 /* light */
1330 if (old_exposure <= VERY_LOW_EXP) {
1331 /* very light */
1332 if (sd->exposure_status == EXPOSURE_VERY_LIGHT)
1333 ++sd->exposure_count;
1334 else {
1335 sd->exposure_status =
1336 EXPOSURE_VERY_LIGHT;
1337 sd->exposure_count = 1;
1339 } else {
1340 /* just light */
1341 if (sd->exposure_status == EXPOSURE_LIGHT)
1342 ++sd->exposure_count;
1343 else {
1344 sd->exposure_status = EXPOSURE_LIGHT;
1345 sd->exposure_count = 1;
1348 } else {
1349 /* not dark or light */
1350 sd->exposure_status = EXPOSURE_NORMAL;
1352 } else {
1353 /* Flicker control off */
1354 if (old_exposure >= MAX_EXP && exp_acc < dark_exp) {
1355 /* dark */
1356 if (exp_acc < very_dark_exp) {
1357 /* very dark */
1358 if (sd->exposure_status == EXPOSURE_VERY_DARK)
1359 ++sd->exposure_count;
1360 else {
1361 sd->exposure_status =
1362 EXPOSURE_VERY_DARK;
1363 sd->exposure_count = 1;
1365 } else {
1366 /* just dark */
1367 if (sd->exposure_status == EXPOSURE_DARK)
1368 ++sd->exposure_count;
1369 else {
1370 sd->exposure_status = EXPOSURE_DARK;
1371 sd->exposure_count = 1;
1374 } else if (old_exposure <= LOW_EXP || exp_acc > light_exp) {
1375 /* light */
1376 if (old_exposure <= VERY_LOW_EXP) {
1377 /* very light */
1378 if (sd->exposure_status == EXPOSURE_VERY_LIGHT)
1379 ++sd->exposure_count;
1380 else {
1381 sd->exposure_status =
1382 EXPOSURE_VERY_LIGHT;
1383 sd->exposure_count = 1;
1385 } else {
1386 /* just light */
1387 if (sd->exposure_status == EXPOSURE_LIGHT)
1388 ++sd->exposure_count;
1389 else {
1390 sd->exposure_status = EXPOSURE_LIGHT;
1391 sd->exposure_count = 1;
1394 } else {
1395 /* not dark or light */
1396 sd->exposure_status = EXPOSURE_NORMAL;
1400 framerate = atomic_read(&sd->fps);
1401 if (framerate > 30 || framerate < 1)
1402 framerate = 1;
1404 if (!sd->params.flickerControl.disabled) {
1405 /* Flicker control on */
1406 if ((sd->exposure_status == EXPOSURE_VERY_DARK ||
1407 sd->exposure_status == EXPOSURE_DARK) &&
1408 sd->exposure_count >= DARK_TIME * framerate &&
1409 sd->params.sensorFps.divisor < 2) {
1411 /* dark for too long */
1412 ++sd->params.sensorFps.divisor;
1413 setfps = 1;
1415 sd->params.flickerControl.coarseJump =
1416 flicker_jumps[sd->mainsFreq]
1417 [sd->params.sensorFps.baserate]
1418 [sd->params.sensorFps.divisor];
1419 setflicker = 1;
1421 new_exposure = sd->params.flickerControl.coarseJump-1;
1422 while (new_exposure < old_exposure / 2)
1423 new_exposure +=
1424 sd->params.flickerControl.coarseJump;
1425 sd->params.exposure.coarseExpLo = new_exposure & 0xff;
1426 sd->params.exposure.coarseExpHi = new_exposure >> 8;
1427 setexp = 1;
1428 sd->exposure_status = EXPOSURE_NORMAL;
1429 PDEBUG(D_CONF, "Automatically decreasing sensor_fps");
1431 } else if ((sd->exposure_status == EXPOSURE_VERY_LIGHT ||
1432 sd->exposure_status == EXPOSURE_LIGHT) &&
1433 sd->exposure_count >= LIGHT_TIME * framerate &&
1434 sd->params.sensorFps.divisor > 0) {
1436 /* light for too long */
1437 int max_exp = FIRMWARE_VERSION(1, 2) ? MAX_EXP_102 :
1438 MAX_EXP;
1439 --sd->params.sensorFps.divisor;
1440 setfps = 1;
1442 sd->params.flickerControl.coarseJump =
1443 flicker_jumps[sd->mainsFreq]
1444 [sd->params.sensorFps.baserate]
1445 [sd->params.sensorFps.divisor];
1446 setflicker = 1;
1448 new_exposure = sd->params.flickerControl.coarseJump-1;
1449 while (new_exposure < 2 * old_exposure &&
1450 new_exposure +
1451 sd->params.flickerControl.coarseJump < max_exp)
1452 new_exposure +=
1453 sd->params.flickerControl.coarseJump;
1454 sd->params.exposure.coarseExpLo = new_exposure & 0xff;
1455 sd->params.exposure.coarseExpHi = new_exposure >> 8;
1456 setexp = 1;
1457 sd->exposure_status = EXPOSURE_NORMAL;
1458 PDEBUG(D_CONF, "Automatically increasing sensor_fps");
1460 } else {
1461 /* Flicker control off */
1462 if ((sd->exposure_status == EXPOSURE_VERY_DARK ||
1463 sd->exposure_status == EXPOSURE_DARK) &&
1464 sd->exposure_count >= DARK_TIME * framerate &&
1465 sd->params.sensorFps.divisor < 2) {
1467 /* dark for too long */
1468 ++sd->params.sensorFps.divisor;
1469 setfps = 1;
1471 if (sd->params.exposure.gain > 0) {
1472 --sd->params.exposure.gain;
1473 setexp = 1;
1475 sd->exposure_status = EXPOSURE_NORMAL;
1476 PDEBUG(D_CONF, "Automatically decreasing sensor_fps");
1478 } else if ((sd->exposure_status == EXPOSURE_VERY_LIGHT ||
1479 sd->exposure_status == EXPOSURE_LIGHT) &&
1480 sd->exposure_count >= LIGHT_TIME * framerate &&
1481 sd->params.sensorFps.divisor > 0) {
1483 /* light for too long */
1484 --sd->params.sensorFps.divisor;
1485 setfps = 1;
1487 if (sd->params.exposure.gain <
1488 sd->params.exposure.gainMode - 1) {
1489 ++sd->params.exposure.gain;
1490 setexp = 1;
1492 sd->exposure_status = EXPOSURE_NORMAL;
1493 PDEBUG(D_CONF, "Automatically increasing sensor_fps");
1497 if (setexp)
1498 command_setexposure(gspca_dev);
1500 if (setfps)
1501 command_setsensorfps(gspca_dev);
1503 if (setflicker)
1504 command_setflickerctrl(gspca_dev);
1507 /*-----------------------------------------------------------------*/
1508 /* if flicker is switched off, this function switches it back on.It checks,
1509 however, that conditions are suitable before restarting it.
1510 This should only be called for firmware version 1.2.
1512 It also adjust the colour balance when an exposure step is detected - as
1513 long as flicker is running
1515 static void restart_flicker(struct gspca_dev *gspca_dev)
1517 struct sd *sd = (struct sd *) gspca_dev;
1518 int cam_exposure, old_exp;
1520 if (!FIRMWARE_VERSION(1, 2))
1521 return;
1523 cam_exposure = atomic_read(&sd->cam_exposure);
1525 if (sd->params.flickerControl.flickerMode == 0 ||
1526 cam_exposure == 0)
1527 return;
1529 old_exp = sd->params.exposure.coarseExpLo +
1530 sd->params.exposure.coarseExpHi*256;
1532 see how far away camera exposure is from a valid
1533 flicker exposure value
1535 cam_exposure %= sd->params.flickerControl.coarseJump;
1536 if (!sd->params.flickerControl.disabled &&
1537 cam_exposure <= sd->params.flickerControl.coarseJump - 3) {
1538 /* Flicker control auto-disabled */
1539 sd->params.flickerControl.disabled = 1;
1542 if (sd->params.flickerControl.disabled &&
1543 old_exp > sd->params.flickerControl.coarseJump +
1544 ROUND_UP_EXP_FOR_FLICKER) {
1545 /* exposure is now high enough to switch
1546 flicker control back on */
1547 set_flicker(gspca_dev, 1, 1);
1551 /* this function is called at probe time */
1552 static int sd_config(struct gspca_dev *gspca_dev,
1553 const struct usb_device_id *id)
1555 struct cam *cam;
1557 reset_camera_params(gspca_dev);
1559 PDEBUG(D_PROBE, "cpia CPiA camera detected (vid/pid 0x%04X:0x%04X)",
1560 id->idVendor, id->idProduct);
1562 cam = &gspca_dev->cam;
1563 cam->cam_mode = mode;
1564 cam->nmodes = ARRAY_SIZE(mode);
1566 sd_setfreq(gspca_dev, FREQ_DEF);
1568 return 0;
1571 /* -- start the camera -- */
1572 static int sd_start(struct gspca_dev *gspca_dev)
1574 struct sd *sd = (struct sd *) gspca_dev;
1575 int priv, ret;
1577 /* Start the camera in low power mode */
1578 if (goto_low_power(gspca_dev)) {
1579 if (sd->params.status.systemState != WARM_BOOT_STATE) {
1580 PDEBUG(D_ERR, "unexpected systemstate: %02x",
1581 sd->params.status.systemState);
1582 printstatus(&sd->params);
1583 return -ENODEV;
1586 /* FIXME: this is just dirty trial and error */
1587 ret = goto_high_power(gspca_dev);
1588 if (ret)
1589 return ret;
1591 ret = do_command(gspca_dev, CPIA_COMMAND_DiscardFrame,
1592 0, 0, 0, 0);
1593 if (ret)
1594 return ret;
1596 ret = goto_low_power(gspca_dev);
1597 if (ret)
1598 return ret;
1601 /* procedure described in developer's guide p3-28 */
1603 /* Check the firmware version. */
1604 sd->params.version.firmwareVersion = 0;
1605 get_version_information(gspca_dev);
1606 if (sd->params.version.firmwareVersion != 1) {
1607 PDEBUG(D_ERR, "only firmware version 1 is supported (got: %d)",
1608 sd->params.version.firmwareVersion);
1609 return -ENODEV;
1612 /* A bug in firmware 1-02 limits gainMode to 2 */
1613 if (sd->params.version.firmwareRevision <= 2 &&
1614 sd->params.exposure.gainMode > 2) {
1615 sd->params.exposure.gainMode = 2;
1618 /* set QX3 detected flag */
1619 sd->params.qx3.qx3_detected = (sd->params.pnpID.vendor == 0x0813 &&
1620 sd->params.pnpID.product == 0x0001);
1622 /* The fatal error checking should be done after
1623 * the camera powers up (developer's guide p 3-38) */
1625 /* Set streamState before transition to high power to avoid bug
1626 * in firmware 1-02 */
1627 ret = do_command(gspca_dev, CPIA_COMMAND_ModifyCameraStatus,
1628 STREAMSTATE, 0, STREAM_NOT_READY, 0);
1629 if (ret)
1630 return ret;
1632 /* GotoHiPower */
1633 ret = goto_high_power(gspca_dev);
1634 if (ret)
1635 return ret;
1637 /* Check the camera status */
1638 ret = do_command(gspca_dev, CPIA_COMMAND_GetCameraStatus, 0, 0, 0, 0);
1639 if (ret)
1640 return ret;
1642 if (sd->params.status.fatalError) {
1643 PDEBUG(D_ERR, "fatal_error: %04x, vp_status: %04x",
1644 sd->params.status.fatalError,
1645 sd->params.status.vpStatus);
1646 return -EIO;
1649 /* VPVersion can't be retrieved before the camera is in HiPower,
1650 * so get it here instead of in get_version_information. */
1651 ret = do_command(gspca_dev, CPIA_COMMAND_GetVPVersion, 0, 0, 0, 0);
1652 if (ret)
1653 return ret;
1655 /* Determine video mode settings */
1656 sd->params.streamStartLine = 120;
1658 priv = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
1659 if (priv & 0x01) { /* crop */
1660 sd->params.roi.colStart = 2;
1661 sd->params.roi.rowStart = 6;
1662 } else {
1663 sd->params.roi.colStart = 0;
1664 sd->params.roi.rowStart = 0;
1667 if (priv & 0x02) { /* quarter */
1668 sd->params.format.videoSize = VIDEOSIZE_QCIF;
1669 sd->params.roi.colStart /= 2;
1670 sd->params.roi.rowStart /= 2;
1671 sd->params.streamStartLine /= 2;
1672 } else
1673 sd->params.format.videoSize = VIDEOSIZE_CIF;
1675 sd->params.roi.colEnd = sd->params.roi.colStart +
1676 (gspca_dev->width >> 3);
1677 sd->params.roi.rowEnd = sd->params.roi.rowStart +
1678 (gspca_dev->height >> 2);
1680 /* And now set the camera to a known state */
1681 ret = do_command(gspca_dev, CPIA_COMMAND_SetGrabMode,
1682 CPIA_GRAB_CONTINEOUS, 0, 0, 0);
1683 if (ret)
1684 return ret;
1685 /* We start with compression disabled, as we need one uncompressed
1686 frame to handle later compressed frames */
1687 ret = do_command(gspca_dev, CPIA_COMMAND_SetCompression,
1688 CPIA_COMPRESSION_NONE,
1689 NO_DECIMATION, 0, 0);
1690 if (ret)
1691 return ret;
1692 ret = command_setcompressiontarget(gspca_dev);
1693 if (ret)
1694 return ret;
1695 ret = command_setcolourparams(gspca_dev);
1696 if (ret)
1697 return ret;
1698 ret = command_setformat(gspca_dev);
1699 if (ret)
1700 return ret;
1701 ret = command_setyuvtresh(gspca_dev);
1702 if (ret)
1703 return ret;
1704 ret = command_setecptiming(gspca_dev);
1705 if (ret)
1706 return ret;
1707 ret = command_setcompressionparams(gspca_dev);
1708 if (ret)
1709 return ret;
1710 ret = command_setexposure(gspca_dev);
1711 if (ret)
1712 return ret;
1713 ret = command_setcolourbalance(gspca_dev);
1714 if (ret)
1715 return ret;
1716 ret = command_setsensorfps(gspca_dev);
1717 if (ret)
1718 return ret;
1719 ret = command_setapcor(gspca_dev);
1720 if (ret)
1721 return ret;
1722 ret = command_setflickerctrl(gspca_dev);
1723 if (ret)
1724 return ret;
1725 ret = command_setvloffset(gspca_dev);
1726 if (ret)
1727 return ret;
1729 /* Start stream */
1730 ret = command_resume(gspca_dev);
1731 if (ret)
1732 return ret;
1734 /* Wait 6 frames before turning compression on for the sensor to get
1735 all settings and AEC/ACB to settle */
1736 sd->first_frame = 6;
1737 sd->exposure_status = EXPOSURE_NORMAL;
1738 sd->exposure_count = 0;
1739 atomic_set(&sd->cam_exposure, 0);
1740 atomic_set(&sd->fps, 0);
1742 return 0;
1745 static void sd_stopN(struct gspca_dev *gspca_dev)
1747 struct sd *sd = (struct sd *) gspca_dev;
1749 command_pause(gspca_dev);
1751 /* save camera state for later open (developers guide ch 3.5.3) */
1752 save_camera_state(gspca_dev);
1754 /* GotoLoPower */
1755 goto_low_power(gspca_dev);
1757 /* Update the camera status */
1758 do_command(gspca_dev, CPIA_COMMAND_GetCameraStatus, 0, 0, 0, 0);
1760 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
1761 /* If the last button state is pressed, release it now! */
1762 if (sd->params.qx3.button) {
1763 /* The camera latch will hold the pressed state until we reset
1764 the latch, so we do not reset sd->params.qx3.button now, to
1765 avoid a false keypress being reported the next sd_start */
1766 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
1767 input_sync(gspca_dev->input_dev);
1769 #endif
1772 /* this function is called at probe and resume time */
1773 static int sd_init(struct gspca_dev *gspca_dev)
1775 #ifdef GSPCA_DEBUG
1776 struct sd *sd = (struct sd *) gspca_dev;
1777 #endif
1778 int ret;
1780 /* Start / Stop the camera to make sure we are talking to
1781 a supported camera, and to get some information from it
1782 to print. */
1783 ret = sd_start(gspca_dev);
1784 if (ret)
1785 return ret;
1787 /* Ensure the QX3 illuminators' states are restored upon resume,
1788 or disable the illuminator controls, if this isn't a QX3 */
1789 if (sd->params.qx3.qx3_detected)
1790 command_setlights(gspca_dev);
1791 else
1792 gspca_dev->ctrl_dis |=
1793 ((1 << ILLUMINATORS_1_IDX) | (1 << ILLUMINATORS_2_IDX));
1795 sd_stopN(gspca_dev);
1797 PDEBUG(D_PROBE, "CPIA Version: %d.%02d (%d.%d)",
1798 sd->params.version.firmwareVersion,
1799 sd->params.version.firmwareRevision,
1800 sd->params.version.vcVersion,
1801 sd->params.version.vcRevision);
1802 PDEBUG(D_PROBE, "CPIA PnP-ID: %04x:%04x:%04x",
1803 sd->params.pnpID.vendor, sd->params.pnpID.product,
1804 sd->params.pnpID.deviceRevision);
1805 PDEBUG(D_PROBE, "VP-Version: %d.%d %04x",
1806 sd->params.vpVersion.vpVersion,
1807 sd->params.vpVersion.vpRevision,
1808 sd->params.vpVersion.cameraHeadID);
1810 return 0;
1813 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1814 u8 *data,
1815 int len)
1817 struct sd *sd = (struct sd *) gspca_dev;
1819 /* Check for SOF */
1820 if (len >= 64 &&
1821 data[0] == MAGIC_0 && data[1] == MAGIC_1 &&
1822 data[16] == sd->params.format.videoSize &&
1823 data[17] == sd->params.format.subSample &&
1824 data[18] == sd->params.format.yuvOrder &&
1825 data[24] == sd->params.roi.colStart &&
1826 data[25] == sd->params.roi.colEnd &&
1827 data[26] == sd->params.roi.rowStart &&
1828 data[27] == sd->params.roi.rowEnd) {
1829 u8 *image;
1831 atomic_set(&sd->cam_exposure, data[39] * 2);
1832 atomic_set(&sd->fps, data[41]);
1834 /* Check for proper EOF for last frame */
1835 image = gspca_dev->image;
1836 if (image != NULL &&
1837 gspca_dev->image_len > 4 &&
1838 image[gspca_dev->image_len - 4] == 0xff &&
1839 image[gspca_dev->image_len - 3] == 0xff &&
1840 image[gspca_dev->image_len - 2] == 0xff &&
1841 image[gspca_dev->image_len - 1] == 0xff)
1842 gspca_frame_add(gspca_dev, LAST_PACKET,
1843 NULL, 0);
1845 gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
1846 return;
1849 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1852 static void sd_dq_callback(struct gspca_dev *gspca_dev)
1854 struct sd *sd = (struct sd *) gspca_dev;
1856 /* Set the normal compression settings once we have captured a
1857 few uncompressed frames (and AEC has hopefully settled) */
1858 if (sd->first_frame) {
1859 sd->first_frame--;
1860 if (sd->first_frame == 0)
1861 command_setcompression(gspca_dev);
1864 /* Switch flicker control back on if it got turned off */
1865 restart_flicker(gspca_dev);
1867 /* If AEC is enabled, monitor the exposure and
1868 adjust the sensor frame rate if needed */
1869 if (sd->params.exposure.expMode == 2)
1870 monitor_exposure(gspca_dev);
1872 /* Update our knowledge of the camera state */
1873 do_command(gspca_dev, CPIA_COMMAND_GetExposure, 0, 0, 0, 0);
1874 do_command(gspca_dev, CPIA_COMMAND_ReadMCPorts, 0, 0, 0, 0);
1877 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
1879 struct sd *sd = (struct sd *) gspca_dev;
1880 int ret;
1882 sd->params.colourParams.brightness = val;
1883 sd->params.flickerControl.allowableOverExposure =
1884 find_over_exposure(sd->params.colourParams.brightness);
1885 if (gspca_dev->streaming) {
1886 ret = command_setcolourparams(gspca_dev);
1887 if (ret)
1888 return ret;
1889 return command_setflickerctrl(gspca_dev);
1891 return 0;
1894 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1896 struct sd *sd = (struct sd *) gspca_dev;
1898 *val = sd->params.colourParams.brightness;
1899 return 0;
1902 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
1904 struct sd *sd = (struct sd *) gspca_dev;
1906 sd->params.colourParams.contrast = val;
1907 if (gspca_dev->streaming)
1908 return command_setcolourparams(gspca_dev);
1910 return 0;
1913 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
1915 struct sd *sd = (struct sd *) gspca_dev;
1917 *val = sd->params.colourParams.contrast;
1918 return 0;
1921 static int sd_setsaturation(struct gspca_dev *gspca_dev, __s32 val)
1923 struct sd *sd = (struct sd *) gspca_dev;
1925 sd->params.colourParams.saturation = val;
1926 if (gspca_dev->streaming)
1927 return command_setcolourparams(gspca_dev);
1929 return 0;
1932 static int sd_getsaturation(struct gspca_dev *gspca_dev, __s32 *val)
1934 struct sd *sd = (struct sd *) gspca_dev;
1936 *val = sd->params.colourParams.saturation;
1937 return 0;
1940 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
1942 struct sd *sd = (struct sd *) gspca_dev;
1943 int on;
1945 switch (val) {
1946 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1947 on = 0;
1948 break;
1949 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1950 on = 1;
1951 sd->mainsFreq = 0;
1952 break;
1953 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
1954 on = 1;
1955 sd->mainsFreq = 1;
1956 break;
1957 default:
1958 return -EINVAL;
1961 sd->freq = val;
1962 sd->params.flickerControl.coarseJump =
1963 flicker_jumps[sd->mainsFreq]
1964 [sd->params.sensorFps.baserate]
1965 [sd->params.sensorFps.divisor];
1967 return set_flicker(gspca_dev, on, gspca_dev->streaming);
1970 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
1972 struct sd *sd = (struct sd *) gspca_dev;
1974 *val = sd->freq;
1975 return 0;
1978 static int sd_setcomptarget(struct gspca_dev *gspca_dev, __s32 val)
1980 struct sd *sd = (struct sd *) gspca_dev;
1982 sd->params.compressionTarget.frTargeting = val;
1983 if (gspca_dev->streaming)
1984 return command_setcompressiontarget(gspca_dev);
1986 return 0;
1989 static int sd_getcomptarget(struct gspca_dev *gspca_dev, __s32 *val)
1991 struct sd *sd = (struct sd *) gspca_dev;
1993 *val = sd->params.compressionTarget.frTargeting;
1994 return 0;
1997 static int sd_setilluminator(struct gspca_dev *gspca_dev, __s32 val, int n)
1999 struct sd *sd = (struct sd *) gspca_dev;
2000 int ret;
2002 if (!sd->params.qx3.qx3_detected)
2003 return -EINVAL;
2005 switch (n) {
2006 case 1:
2007 sd->params.qx3.bottomlight = val ? 1 : 0;
2008 break;
2009 case 2:
2010 sd->params.qx3.toplight = val ? 1 : 0;
2011 break;
2012 default:
2013 return -EINVAL;
2016 ret = command_setlights(gspca_dev);
2017 if (ret && ret != -EINVAL)
2018 ret = -EBUSY;
2020 return ret;
2023 static int sd_setilluminator1(struct gspca_dev *gspca_dev, __s32 val)
2025 return sd_setilluminator(gspca_dev, val, 1);
2028 static int sd_setilluminator2(struct gspca_dev *gspca_dev, __s32 val)
2030 return sd_setilluminator(gspca_dev, val, 2);
2033 static int sd_getilluminator(struct gspca_dev *gspca_dev, __s32 *val, int n)
2035 struct sd *sd = (struct sd *) gspca_dev;
2037 if (!sd->params.qx3.qx3_detected)
2038 return -EINVAL;
2040 switch (n) {
2041 case 1:
2042 *val = sd->params.qx3.bottomlight;
2043 break;
2044 case 2:
2045 *val = sd->params.qx3.toplight;
2046 break;
2047 default:
2048 return -EINVAL;
2050 return 0;
2053 static int sd_getilluminator1(struct gspca_dev *gspca_dev, __s32 *val)
2055 return sd_getilluminator(gspca_dev, val, 1);
2058 static int sd_getilluminator2(struct gspca_dev *gspca_dev, __s32 *val)
2060 return sd_getilluminator(gspca_dev, val, 2);
2063 static int sd_querymenu(struct gspca_dev *gspca_dev,
2064 struct v4l2_querymenu *menu)
2066 switch (menu->id) {
2067 case V4L2_CID_POWER_LINE_FREQUENCY:
2068 switch (menu->index) {
2069 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
2070 strcpy((char *) menu->name, "NoFliker");
2071 return 0;
2072 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
2073 strcpy((char *) menu->name, "50 Hz");
2074 return 0;
2075 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
2076 strcpy((char *) menu->name, "60 Hz");
2077 return 0;
2079 break;
2080 case V4L2_CID_COMP_TARGET:
2081 switch (menu->index) {
2082 case CPIA_COMPRESSION_TARGET_QUALITY:
2083 strcpy((char *) menu->name, "Quality");
2084 return 0;
2085 case CPIA_COMPRESSION_TARGET_FRAMERATE:
2086 strcpy((char *) menu->name, "Framerate");
2087 return 0;
2089 break;
2091 return -EINVAL;
2094 /* sub-driver description */
2095 static const struct sd_desc sd_desc = {
2096 .name = MODULE_NAME,
2097 .ctrls = sd_ctrls,
2098 .nctrls = ARRAY_SIZE(sd_ctrls),
2099 .config = sd_config,
2100 .init = sd_init,
2101 .start = sd_start,
2102 .stopN = sd_stopN,
2103 .dq_callback = sd_dq_callback,
2104 .pkt_scan = sd_pkt_scan,
2105 .querymenu = sd_querymenu,
2106 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2107 .other_input = 1,
2108 #endif
2111 /* -- module initialisation -- */
2112 static const struct usb_device_id device_table[] = {
2113 {USB_DEVICE(0x0553, 0x0002)},
2114 {USB_DEVICE(0x0813, 0x0001)},
2117 MODULE_DEVICE_TABLE(usb, device_table);
2119 /* -- device connect -- */
2120 static int sd_probe(struct usb_interface *intf,
2121 const struct usb_device_id *id)
2123 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
2124 THIS_MODULE);
2127 static struct usb_driver sd_driver = {
2128 .name = MODULE_NAME,
2129 .id_table = device_table,
2130 .probe = sd_probe,
2131 .disconnect = gspca_disconnect,
2132 #ifdef CONFIG_PM
2133 .suspend = gspca_suspend,
2134 .resume = gspca_resume,
2135 #endif
2138 /* -- module insert / remove -- */
2139 static int __init sd_mod_init(void)
2141 return usb_register(&sd_driver);
2143 static void __exit sd_mod_exit(void)
2145 usb_deregister(&sd_driver);
2148 module_init(sd_mod_init);
2149 module_exit(sd_mod_exit);