cpia1.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  1. /*
  2. * cpia CPiA (1) gspca driver
  3. *
  4. * Copyright (C) 2010-2011 Hans de Goede <hdegoede@redhat.com>
  5. *
  6. * This module is adapted from the in kernel v4l1 cpia driver which is :
  7. *
  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
  12. *
  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.
  17. *
  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.
  22. *
  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
  26. *
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #define MODULE_NAME "cpia1"
  30. #include <linux/input.h>
  31. #include "gspca.h"
  32. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  33. MODULE_DESCRIPTION("Vision CPiA");
  34. MODULE_LICENSE("GPL");
  35. /* constant value's */
  36. #define MAGIC_0 0x19
  37. #define MAGIC_1 0x68
  38. #define DATA_IN 0xc0
  39. #define DATA_OUT 0x40
  40. #define VIDEOSIZE_QCIF 0 /* 176x144 */
  41. #define VIDEOSIZE_CIF 1 /* 352x288 */
  42. #define SUBSAMPLE_420 0
  43. #define SUBSAMPLE_422 1
  44. #define YUVORDER_YUYV 0
  45. #define YUVORDER_UYVY 1
  46. #define NOT_COMPRESSED 0
  47. #define COMPRESSED 1
  48. #define NO_DECIMATION 0
  49. #define DECIMATION_ENAB 1
  50. #define EOI 0xff /* End Of Image */
  51. #define EOL 0xfd /* End Of Line */
  52. #define FRAME_HEADER_SIZE 64
  53. /* Image grab modes */
  54. #define CPIA_GRAB_SINGLE 0
  55. #define CPIA_GRAB_CONTINEOUS 1
  56. /* Compression parameters */
  57. #define CPIA_COMPRESSION_NONE 0
  58. #define CPIA_COMPRESSION_AUTO 1
  59. #define CPIA_COMPRESSION_MANUAL 2
  60. #define CPIA_COMPRESSION_TARGET_QUALITY 0
  61. #define CPIA_COMPRESSION_TARGET_FRAMERATE 1
  62. /* Return offsets for GetCameraState */
  63. #define SYSTEMSTATE 0
  64. #define GRABSTATE 1
  65. #define STREAMSTATE 2
  66. #define FATALERROR 3
  67. #define CMDERROR 4
  68. #define DEBUGFLAGS 5
  69. #define VPSTATUS 6
  70. #define ERRORCODE 7
  71. /* SystemState */
  72. #define UNINITIALISED_STATE 0
  73. #define PASS_THROUGH_STATE 1
  74. #define LO_POWER_STATE 2
  75. #define HI_POWER_STATE 3
  76. #define WARM_BOOT_STATE 4
  77. /* GrabState */
  78. #define GRAB_IDLE 0
  79. #define GRAB_ACTIVE 1
  80. #define GRAB_DONE 2
  81. /* StreamState */
  82. #define STREAM_NOT_READY 0
  83. #define STREAM_READY 1
  84. #define STREAM_OPEN 2
  85. #define STREAM_PAUSED 3
  86. #define STREAM_FINISHED 4
  87. /* Fatal Error, CmdError, and DebugFlags */
  88. #define CPIA_FLAG 1
  89. #define SYSTEM_FLAG 2
  90. #define INT_CTRL_FLAG 4
  91. #define PROCESS_FLAG 8
  92. #define COM_FLAG 16
  93. #define VP_CTRL_FLAG 32
  94. #define CAPTURE_FLAG 64
  95. #define DEBUG_FLAG 128
  96. /* VPStatus */
  97. #define VP_STATE_OK 0x00
  98. #define VP_STATE_FAILED_VIDEOINIT 0x01
  99. #define VP_STATE_FAILED_AECACBINIT 0x02
  100. #define VP_STATE_AEC_MAX 0x04
  101. #define VP_STATE_ACB_BMAX 0x08
  102. #define VP_STATE_ACB_RMIN 0x10
  103. #define VP_STATE_ACB_GMIN 0x20
  104. #define VP_STATE_ACB_RMAX 0x40
  105. #define VP_STATE_ACB_GMAX 0x80
  106. /* default (minimum) compensation values */
  107. #define COMP_RED 220
  108. #define COMP_GREEN1 214
  109. #define COMP_GREEN2 COMP_GREEN1
  110. #define COMP_BLUE 230
  111. /* exposure status */
  112. #define EXPOSURE_VERY_LIGHT 0
  113. #define EXPOSURE_LIGHT 1
  114. #define EXPOSURE_NORMAL 2
  115. #define EXPOSURE_DARK 3
  116. #define EXPOSURE_VERY_DARK 4
  117. #define CPIA_MODULE_CPIA (0 << 5)
  118. #define CPIA_MODULE_SYSTEM (1 << 5)
  119. #define CPIA_MODULE_VP_CTRL (5 << 5)
  120. #define CPIA_MODULE_CAPTURE (6 << 5)
  121. #define CPIA_MODULE_DEBUG (7 << 5)
  122. #define INPUT (DATA_IN << 8)
  123. #define OUTPUT (DATA_OUT << 8)
  124. #define CPIA_COMMAND_GetCPIAVersion (INPUT | CPIA_MODULE_CPIA | 1)
  125. #define CPIA_COMMAND_GetPnPID (INPUT | CPIA_MODULE_CPIA | 2)
  126. #define CPIA_COMMAND_GetCameraStatus (INPUT | CPIA_MODULE_CPIA | 3)
  127. #define CPIA_COMMAND_GotoHiPower (OUTPUT | CPIA_MODULE_CPIA | 4)
  128. #define CPIA_COMMAND_GotoLoPower (OUTPUT | CPIA_MODULE_CPIA | 5)
  129. #define CPIA_COMMAND_GotoSuspend (OUTPUT | CPIA_MODULE_CPIA | 7)
  130. #define CPIA_COMMAND_GotoPassThrough (OUTPUT | CPIA_MODULE_CPIA | 8)
  131. #define CPIA_COMMAND_ModifyCameraStatus (OUTPUT | CPIA_MODULE_CPIA | 10)
  132. #define CPIA_COMMAND_ReadVCRegs (INPUT | CPIA_MODULE_SYSTEM | 1)
  133. #define CPIA_COMMAND_WriteVCReg (OUTPUT | CPIA_MODULE_SYSTEM | 2)
  134. #define CPIA_COMMAND_ReadMCPorts (INPUT | CPIA_MODULE_SYSTEM | 3)
  135. #define CPIA_COMMAND_WriteMCPort (OUTPUT | CPIA_MODULE_SYSTEM | 4)
  136. #define CPIA_COMMAND_SetBaudRate (OUTPUT | CPIA_MODULE_SYSTEM | 5)
  137. #define CPIA_COMMAND_SetECPTiming (OUTPUT | CPIA_MODULE_SYSTEM | 6)
  138. #define CPIA_COMMAND_ReadIDATA (INPUT | CPIA_MODULE_SYSTEM | 7)
  139. #define CPIA_COMMAND_WriteIDATA (OUTPUT | CPIA_MODULE_SYSTEM | 8)
  140. #define CPIA_COMMAND_GenericCall (OUTPUT | CPIA_MODULE_SYSTEM | 9)
  141. #define CPIA_COMMAND_I2CStart (OUTPUT | CPIA_MODULE_SYSTEM | 10)
  142. #define CPIA_COMMAND_I2CStop (OUTPUT | CPIA_MODULE_SYSTEM | 11)
  143. #define CPIA_COMMAND_I2CWrite (OUTPUT | CPIA_MODULE_SYSTEM | 12)
  144. #define CPIA_COMMAND_I2CRead (INPUT | CPIA_MODULE_SYSTEM | 13)
  145. #define CPIA_COMMAND_GetVPVersion (INPUT | CPIA_MODULE_VP_CTRL | 1)
  146. #define CPIA_COMMAND_ResetFrameCounter (INPUT | CPIA_MODULE_VP_CTRL | 2)
  147. #define CPIA_COMMAND_SetColourParams (OUTPUT | CPIA_MODULE_VP_CTRL | 3)
  148. #define CPIA_COMMAND_SetExposure (OUTPUT | CPIA_MODULE_VP_CTRL | 4)
  149. #define CPIA_COMMAND_SetColourBalance (OUTPUT | CPIA_MODULE_VP_CTRL | 6)
  150. #define CPIA_COMMAND_SetSensorFPS (OUTPUT | CPIA_MODULE_VP_CTRL | 7)
  151. #define CPIA_COMMAND_SetVPDefaults (OUTPUT | CPIA_MODULE_VP_CTRL | 8)
  152. #define CPIA_COMMAND_SetApcor (OUTPUT | CPIA_MODULE_VP_CTRL | 9)
  153. #define CPIA_COMMAND_SetFlickerCtrl (OUTPUT | CPIA_MODULE_VP_CTRL | 10)
  154. #define CPIA_COMMAND_SetVLOffset (OUTPUT | CPIA_MODULE_VP_CTRL | 11)
  155. #define CPIA_COMMAND_GetColourParams (INPUT | CPIA_MODULE_VP_CTRL | 16)
  156. #define CPIA_COMMAND_GetColourBalance (INPUT | CPIA_MODULE_VP_CTRL | 17)
  157. #define CPIA_COMMAND_GetExposure (INPUT | CPIA_MODULE_VP_CTRL | 18)
  158. #define CPIA_COMMAND_SetSensorMatrix (OUTPUT | CPIA_MODULE_VP_CTRL | 19)
  159. #define CPIA_COMMAND_ColourBars (OUTPUT | CPIA_MODULE_VP_CTRL | 25)
  160. #define CPIA_COMMAND_ReadVPRegs (INPUT | CPIA_MODULE_VP_CTRL | 30)
  161. #define CPIA_COMMAND_WriteVPReg (OUTPUT | CPIA_MODULE_VP_CTRL | 31)
  162. #define CPIA_COMMAND_GrabFrame (OUTPUT | CPIA_MODULE_CAPTURE | 1)
  163. #define CPIA_COMMAND_UploadFrame (OUTPUT | CPIA_MODULE_CAPTURE | 2)
  164. #define CPIA_COMMAND_SetGrabMode (OUTPUT | CPIA_MODULE_CAPTURE | 3)
  165. #define CPIA_COMMAND_InitStreamCap (OUTPUT | CPIA_MODULE_CAPTURE | 4)
  166. #define CPIA_COMMAND_FiniStreamCap (OUTPUT | CPIA_MODULE_CAPTURE | 5)
  167. #define CPIA_COMMAND_StartStreamCap (OUTPUT | CPIA_MODULE_CAPTURE | 6)
  168. #define CPIA_COMMAND_EndStreamCap (OUTPUT | CPIA_MODULE_CAPTURE | 7)
  169. #define CPIA_COMMAND_SetFormat (OUTPUT | CPIA_MODULE_CAPTURE | 8)
  170. #define CPIA_COMMAND_SetROI (OUTPUT | CPIA_MODULE_CAPTURE | 9)
  171. #define CPIA_COMMAND_SetCompression (OUTPUT | CPIA_MODULE_CAPTURE | 10)
  172. #define CPIA_COMMAND_SetCompressionTarget (OUTPUT | CPIA_MODULE_CAPTURE | 11)
  173. #define CPIA_COMMAND_SetYUVThresh (OUTPUT | CPIA_MODULE_CAPTURE | 12)
  174. #define CPIA_COMMAND_SetCompressionParams (OUTPUT | CPIA_MODULE_CAPTURE | 13)
  175. #define CPIA_COMMAND_DiscardFrame (OUTPUT | CPIA_MODULE_CAPTURE | 14)
  176. #define CPIA_COMMAND_GrabReset (OUTPUT | CPIA_MODULE_CAPTURE | 15)
  177. #define CPIA_COMMAND_OutputRS232 (OUTPUT | CPIA_MODULE_DEBUG | 1)
  178. #define CPIA_COMMAND_AbortProcess (OUTPUT | CPIA_MODULE_DEBUG | 4)
  179. #define CPIA_COMMAND_SetDramPage (OUTPUT | CPIA_MODULE_DEBUG | 5)
  180. #define CPIA_COMMAND_StartDramUpload (OUTPUT | CPIA_MODULE_DEBUG | 6)
  181. #define CPIA_COMMAND_StartDummyDtream (OUTPUT | CPIA_MODULE_DEBUG | 8)
  182. #define CPIA_COMMAND_AbortStream (OUTPUT | CPIA_MODULE_DEBUG | 9)
  183. #define CPIA_COMMAND_DownloadDRAM (OUTPUT | CPIA_MODULE_DEBUG | 10)
  184. #define CPIA_COMMAND_Null (OUTPUT | CPIA_MODULE_DEBUG | 11)
  185. #define ROUND_UP_EXP_FOR_FLICKER 15
  186. /* Constants for automatic frame rate adjustment */
  187. #define MAX_EXP 302
  188. #define MAX_EXP_102 255
  189. #define LOW_EXP 140
  190. #define VERY_LOW_EXP 70
  191. #define TC 94
  192. #define EXP_ACC_DARK 50
  193. #define EXP_ACC_LIGHT 90
  194. #define HIGH_COMP_102 160
  195. #define MAX_COMP 239
  196. #define DARK_TIME 3
  197. #define LIGHT_TIME 3
  198. #define FIRMWARE_VERSION(x, y) (sd->params.version.firmwareVersion == (x) && \
  199. sd->params.version.firmwareRevision == (y))
  200. #define CPIA1_CID_COMP_TARGET (V4L2_CTRL_CLASS_USER + 0x1000)
  201. #define BRIGHTNESS_DEF 50
  202. #define CONTRAST_DEF 48
  203. #define SATURATION_DEF 50
  204. #define FREQ_DEF V4L2_CID_POWER_LINE_FREQUENCY_50HZ
  205. #define ILLUMINATORS_1_DEF 0
  206. #define ILLUMINATORS_2_DEF 0
  207. #define COMP_TARGET_DEF CPIA_COMPRESSION_TARGET_QUALITY
  208. /* Developer's Guide Table 5 p 3-34
  209. * indexed by [mains][sensorFps.baserate][sensorFps.divisor]*/
  210. static u8 flicker_jumps[2][2][4] =
  211. { { { 76, 38, 19, 9 }, { 92, 46, 23, 11 } },
  212. { { 64, 32, 16, 8 }, { 76, 38, 19, 9} }
  213. };
  214. struct cam_params {
  215. struct {
  216. u8 firmwareVersion;
  217. u8 firmwareRevision;
  218. u8 vcVersion;
  219. u8 vcRevision;
  220. } version;
  221. struct {
  222. u16 vendor;
  223. u16 product;
  224. u16 deviceRevision;
  225. } pnpID;
  226. struct {
  227. u8 vpVersion;
  228. u8 vpRevision;
  229. u16 cameraHeadID;
  230. } vpVersion;
  231. struct {
  232. u8 systemState;
  233. u8 grabState;
  234. u8 streamState;
  235. u8 fatalError;
  236. u8 cmdError;
  237. u8 debugFlags;
  238. u8 vpStatus;
  239. u8 errorCode;
  240. } status;
  241. struct {
  242. u8 brightness;
  243. u8 contrast;
  244. u8 saturation;
  245. } colourParams;
  246. struct {
  247. u8 gainMode;
  248. u8 expMode;
  249. u8 compMode;
  250. u8 centreWeight;
  251. u8 gain;
  252. u8 fineExp;
  253. u8 coarseExpLo;
  254. u8 coarseExpHi;
  255. u8 redComp;
  256. u8 green1Comp;
  257. u8 green2Comp;
  258. u8 blueComp;
  259. } exposure;
  260. struct {
  261. u8 balanceMode;
  262. u8 redGain;
  263. u8 greenGain;
  264. u8 blueGain;
  265. } colourBalance;
  266. struct {
  267. u8 divisor;
  268. u8 baserate;
  269. } sensorFps;
  270. struct {
  271. u8 gain1;
  272. u8 gain2;
  273. u8 gain4;
  274. u8 gain8;
  275. } apcor;
  276. struct {
  277. u8 disabled;
  278. u8 flickerMode;
  279. u8 coarseJump;
  280. u8 allowableOverExposure;
  281. } flickerControl;
  282. struct {
  283. u8 gain1;
  284. u8 gain2;
  285. u8 gain4;
  286. u8 gain8;
  287. } vlOffset;
  288. struct {
  289. u8 mode;
  290. u8 decimation;
  291. } compression;
  292. struct {
  293. u8 frTargeting;
  294. u8 targetFR;
  295. u8 targetQ;
  296. } compressionTarget;
  297. struct {
  298. u8 yThreshold;
  299. u8 uvThreshold;
  300. } yuvThreshold;
  301. struct {
  302. u8 hysteresis;
  303. u8 threshMax;
  304. u8 smallStep;
  305. u8 largeStep;
  306. u8 decimationHysteresis;
  307. u8 frDiffStepThresh;
  308. u8 qDiffStepThresh;
  309. u8 decimationThreshMod;
  310. } compressionParams;
  311. struct {
  312. u8 videoSize; /* CIF/QCIF */
  313. u8 subSample;
  314. u8 yuvOrder;
  315. } format;
  316. struct { /* Intel QX3 specific data */
  317. u8 qx3_detected; /* a QX3 is present */
  318. u8 toplight; /* top light lit , R/W */
  319. u8 bottomlight; /* bottom light lit, R/W */
  320. u8 button; /* snapshot button pressed (R/O) */
  321. u8 cradled; /* microscope is in cradle (R/O) */
  322. } qx3;
  323. struct {
  324. u8 colStart; /* skip first 8*colStart pixels */
  325. u8 colEnd; /* finish at 8*colEnd pixels */
  326. u8 rowStart; /* skip first 4*rowStart lines */
  327. u8 rowEnd; /* finish at 4*rowEnd lines */
  328. } roi;
  329. u8 ecpTiming;
  330. u8 streamStartLine;
  331. };
  332. /* specific webcam descriptor */
  333. struct sd {
  334. struct gspca_dev gspca_dev; /* !! must be the first item */
  335. struct cam_params params; /* camera settings */
  336. atomic_t cam_exposure;
  337. atomic_t fps;
  338. int exposure_count;
  339. u8 exposure_status;
  340. struct v4l2_ctrl *freq;
  341. u8 mainsFreq; /* 0 = 50hz, 1 = 60hz */
  342. u8 first_frame;
  343. };
  344. static const struct v4l2_pix_format mode[] = {
  345. {160, 120, V4L2_PIX_FMT_CPIA1, V4L2_FIELD_NONE,
  346. /* The sizeimage is trial and error, as with low framerates
  347. the camera will pad out usb frames, making the image
  348. data larger then strictly necessary */
  349. .bytesperline = 160,
  350. .sizeimage = 65536,
  351. .colorspace = V4L2_COLORSPACE_SRGB,
  352. .priv = 3},
  353. {176, 144, V4L2_PIX_FMT_CPIA1, V4L2_FIELD_NONE,
  354. .bytesperline = 172,
  355. .sizeimage = 65536,
  356. .colorspace = V4L2_COLORSPACE_SRGB,
  357. .priv = 2},
  358. {320, 240, V4L2_PIX_FMT_CPIA1, V4L2_FIELD_NONE,
  359. .bytesperline = 320,
  360. .sizeimage = 262144,
  361. .colorspace = V4L2_COLORSPACE_SRGB,
  362. .priv = 1},
  363. {352, 288, V4L2_PIX_FMT_CPIA1, V4L2_FIELD_NONE,
  364. .bytesperline = 352,
  365. .sizeimage = 262144,
  366. .colorspace = V4L2_COLORSPACE_SRGB,
  367. .priv = 0},
  368. };
  369. /**********************************************************************
  370. *
  371. * General functions
  372. *
  373. **********************************************************************/
  374. static int cpia_usb_transferCmd(struct gspca_dev *gspca_dev, u8 *command)
  375. {
  376. u8 requesttype;
  377. unsigned int pipe;
  378. int ret, databytes = command[6] | (command[7] << 8);
  379. /* Sometimes we see spurious EPIPE errors */
  380. int retries = 3;
  381. if (command[0] == DATA_IN) {
  382. pipe = usb_rcvctrlpipe(gspca_dev->dev, 0);
  383. requesttype = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
  384. } else if (command[0] == DATA_OUT) {
  385. pipe = usb_sndctrlpipe(gspca_dev->dev, 0);
  386. requesttype = USB_TYPE_VENDOR | USB_RECIP_DEVICE;
  387. } else {
  388. PERR("Unexpected first byte of command: %x", command[0]);
  389. return -EINVAL;
  390. }
  391. retry:
  392. ret = usb_control_msg(gspca_dev->dev, pipe,
  393. command[1],
  394. requesttype,
  395. command[2] | (command[3] << 8),
  396. command[4] | (command[5] << 8),
  397. gspca_dev->usb_buf, databytes, 1000);
  398. if (ret < 0)
  399. pr_err("usb_control_msg %02x, error %d\n", command[1], ret);
  400. if (ret == -EPIPE && retries > 0) {
  401. retries--;
  402. goto retry;
  403. }
  404. return (ret < 0) ? ret : 0;
  405. }
  406. /* send an arbitrary command to the camera */
  407. static int do_command(struct gspca_dev *gspca_dev, u16 command,
  408. u8 a, u8 b, u8 c, u8 d)
  409. {
  410. struct sd *sd = (struct sd *) gspca_dev;
  411. int ret, datasize;
  412. u8 cmd[8];
  413. switch (command) {
  414. case CPIA_COMMAND_GetCPIAVersion:
  415. case CPIA_COMMAND_GetPnPID:
  416. case CPIA_COMMAND_GetCameraStatus:
  417. case CPIA_COMMAND_GetVPVersion:
  418. case CPIA_COMMAND_GetColourParams:
  419. case CPIA_COMMAND_GetColourBalance:
  420. case CPIA_COMMAND_GetExposure:
  421. datasize = 8;
  422. break;
  423. case CPIA_COMMAND_ReadMCPorts:
  424. case CPIA_COMMAND_ReadVCRegs:
  425. datasize = 4;
  426. break;
  427. default:
  428. datasize = 0;
  429. break;
  430. }
  431. cmd[0] = command >> 8;
  432. cmd[1] = command & 0xff;
  433. cmd[2] = a;
  434. cmd[3] = b;
  435. cmd[4] = c;
  436. cmd[5] = d;
  437. cmd[6] = datasize;
  438. cmd[7] = 0;
  439. ret = cpia_usb_transferCmd(gspca_dev, cmd);
  440. if (ret)
  441. return ret;
  442. switch (command) {
  443. case CPIA_COMMAND_GetCPIAVersion:
  444. sd->params.version.firmwareVersion = gspca_dev->usb_buf[0];
  445. sd->params.version.firmwareRevision = gspca_dev->usb_buf[1];
  446. sd->params.version.vcVersion = gspca_dev->usb_buf[2];
  447. sd->params.version.vcRevision = gspca_dev->usb_buf[3];
  448. break;
  449. case CPIA_COMMAND_GetPnPID:
  450. sd->params.pnpID.vendor =
  451. gspca_dev->usb_buf[0] | (gspca_dev->usb_buf[1] << 8);
  452. sd->params.pnpID.product =
  453. gspca_dev->usb_buf[2] | (gspca_dev->usb_buf[3] << 8);
  454. sd->params.pnpID.deviceRevision =
  455. gspca_dev->usb_buf[4] | (gspca_dev->usb_buf[5] << 8);
  456. break;
  457. case CPIA_COMMAND_GetCameraStatus:
  458. sd->params.status.systemState = gspca_dev->usb_buf[0];
  459. sd->params.status.grabState = gspca_dev->usb_buf[1];
  460. sd->params.status.streamState = gspca_dev->usb_buf[2];
  461. sd->params.status.fatalError = gspca_dev->usb_buf[3];
  462. sd->params.status.cmdError = gspca_dev->usb_buf[4];
  463. sd->params.status.debugFlags = gspca_dev->usb_buf[5];
  464. sd->params.status.vpStatus = gspca_dev->usb_buf[6];
  465. sd->params.status.errorCode = gspca_dev->usb_buf[7];
  466. break;
  467. case CPIA_COMMAND_GetVPVersion:
  468. sd->params.vpVersion.vpVersion = gspca_dev->usb_buf[0];
  469. sd->params.vpVersion.vpRevision = gspca_dev->usb_buf[1];
  470. sd->params.vpVersion.cameraHeadID =
  471. gspca_dev->usb_buf[2] | (gspca_dev->usb_buf[3] << 8);
  472. break;
  473. case CPIA_COMMAND_GetColourParams:
  474. sd->params.colourParams.brightness = gspca_dev->usb_buf[0];
  475. sd->params.colourParams.contrast = gspca_dev->usb_buf[1];
  476. sd->params.colourParams.saturation = gspca_dev->usb_buf[2];
  477. break;
  478. case CPIA_COMMAND_GetColourBalance:
  479. sd->params.colourBalance.redGain = gspca_dev->usb_buf[0];
  480. sd->params.colourBalance.greenGain = gspca_dev->usb_buf[1];
  481. sd->params.colourBalance.blueGain = gspca_dev->usb_buf[2];
  482. break;
  483. case CPIA_COMMAND_GetExposure:
  484. sd->params.exposure.gain = gspca_dev->usb_buf[0];
  485. sd->params.exposure.fineExp = gspca_dev->usb_buf[1];
  486. sd->params.exposure.coarseExpLo = gspca_dev->usb_buf[2];
  487. sd->params.exposure.coarseExpHi = gspca_dev->usb_buf[3];
  488. sd->params.exposure.redComp = gspca_dev->usb_buf[4];
  489. sd->params.exposure.green1Comp = gspca_dev->usb_buf[5];
  490. sd->params.exposure.green2Comp = gspca_dev->usb_buf[6];
  491. sd->params.exposure.blueComp = gspca_dev->usb_buf[7];
  492. break;
  493. case CPIA_COMMAND_ReadMCPorts:
  494. /* test button press */
  495. a = ((gspca_dev->usb_buf[1] & 0x02) == 0);
  496. if (a != sd->params.qx3.button) {
  497. #if IS_ENABLED(CONFIG_INPUT)
  498. input_report_key(gspca_dev->input_dev, KEY_CAMERA, a);
  499. input_sync(gspca_dev->input_dev);
  500. #endif
  501. sd->params.qx3.button = a;
  502. }
  503. if (sd->params.qx3.button) {
  504. /* button pressed - unlock the latch */
  505. do_command(gspca_dev, CPIA_COMMAND_WriteMCPort,
  506. 3, 0xdf, 0xdf, 0);
  507. do_command(gspca_dev, CPIA_COMMAND_WriteMCPort,
  508. 3, 0xff, 0xff, 0);
  509. }
  510. /* test whether microscope is cradled */
  511. sd->params.qx3.cradled = ((gspca_dev->usb_buf[2] & 0x40) == 0);
  512. break;
  513. }
  514. return 0;
  515. }
  516. /* send a command to the camera with an additional data transaction */
  517. static int do_command_extended(struct gspca_dev *gspca_dev, u16 command,
  518. u8 a, u8 b, u8 c, u8 d,
  519. u8 e, u8 f, u8 g, u8 h,
  520. u8 i, u8 j, u8 k, u8 l)
  521. {
  522. u8 cmd[8];
  523. cmd[0] = command >> 8;
  524. cmd[1] = command & 0xff;
  525. cmd[2] = a;
  526. cmd[3] = b;
  527. cmd[4] = c;
  528. cmd[5] = d;
  529. cmd[6] = 8;
  530. cmd[7] = 0;
  531. gspca_dev->usb_buf[0] = e;
  532. gspca_dev->usb_buf[1] = f;
  533. gspca_dev->usb_buf[2] = g;
  534. gspca_dev->usb_buf[3] = h;
  535. gspca_dev->usb_buf[4] = i;
  536. gspca_dev->usb_buf[5] = j;
  537. gspca_dev->usb_buf[6] = k;
  538. gspca_dev->usb_buf[7] = l;
  539. return cpia_usb_transferCmd(gspca_dev, cmd);
  540. }
  541. /* find_over_exposure
  542. * Finds a suitable value of OverExposure for use with SetFlickerCtrl
  543. * Some calculation is required because this value changes with the brightness
  544. * set with SetColourParameters
  545. *
  546. * Parameters: Brightness - last brightness value set with SetColourParameters
  547. *
  548. * Returns: OverExposure value to use with SetFlickerCtrl
  549. */
  550. #define FLICKER_MAX_EXPOSURE 250
  551. #define FLICKER_ALLOWABLE_OVER_EXPOSURE 146
  552. #define FLICKER_BRIGHTNESS_CONSTANT 59
  553. static int find_over_exposure(int brightness)
  554. {
  555. int MaxAllowableOverExposure, OverExposure;
  556. MaxAllowableOverExposure = FLICKER_MAX_EXPOSURE - brightness -
  557. FLICKER_BRIGHTNESS_CONSTANT;
  558. if (MaxAllowableOverExposure < FLICKER_ALLOWABLE_OVER_EXPOSURE)
  559. OverExposure = MaxAllowableOverExposure;
  560. else
  561. OverExposure = FLICKER_ALLOWABLE_OVER_EXPOSURE;
  562. return OverExposure;
  563. }
  564. #undef FLICKER_MAX_EXPOSURE
  565. #undef FLICKER_ALLOWABLE_OVER_EXPOSURE
  566. #undef FLICKER_BRIGHTNESS_CONSTANT
  567. /* initialise cam_data structure */
  568. static void reset_camera_params(struct gspca_dev *gspca_dev)
  569. {
  570. struct sd *sd = (struct sd *) gspca_dev;
  571. struct cam_params *params = &sd->params;
  572. /* The following parameter values are the defaults from
  573. * "Software Developer's Guide for CPiA Cameras". Any changes
  574. * to the defaults are noted in comments. */
  575. params->colourParams.brightness = BRIGHTNESS_DEF;
  576. params->colourParams.contrast = CONTRAST_DEF;
  577. params->colourParams.saturation = SATURATION_DEF;
  578. params->exposure.gainMode = 4;
  579. params->exposure.expMode = 2; /* AEC */
  580. params->exposure.compMode = 1;
  581. params->exposure.centreWeight = 1;
  582. params->exposure.gain = 0;
  583. params->exposure.fineExp = 0;
  584. params->exposure.coarseExpLo = 185;
  585. params->exposure.coarseExpHi = 0;
  586. params->exposure.redComp = COMP_RED;
  587. params->exposure.green1Comp = COMP_GREEN1;
  588. params->exposure.green2Comp = COMP_GREEN2;
  589. params->exposure.blueComp = COMP_BLUE;
  590. params->colourBalance.balanceMode = 2; /* ACB */
  591. params->colourBalance.redGain = 32;
  592. params->colourBalance.greenGain = 6;
  593. params->colourBalance.blueGain = 92;
  594. params->apcor.gain1 = 0x18;
  595. params->apcor.gain2 = 0x16;
  596. params->apcor.gain4 = 0x24;
  597. params->apcor.gain8 = 0x34;
  598. params->vlOffset.gain1 = 20;
  599. params->vlOffset.gain2 = 24;
  600. params->vlOffset.gain4 = 26;
  601. params->vlOffset.gain8 = 26;
  602. params->compressionParams.hysteresis = 3;
  603. params->compressionParams.threshMax = 11;
  604. params->compressionParams.smallStep = 1;
  605. params->compressionParams.largeStep = 3;
  606. params->compressionParams.decimationHysteresis = 2;
  607. params->compressionParams.frDiffStepThresh = 5;
  608. params->compressionParams.qDiffStepThresh = 3;
  609. params->compressionParams.decimationThreshMod = 2;
  610. /* End of default values from Software Developer's Guide */
  611. /* Set Sensor FPS to 15fps. This seems better than 30fps
  612. * for indoor lighting. */
  613. params->sensorFps.divisor = 1;
  614. params->sensorFps.baserate = 1;
  615. params->flickerControl.flickerMode = 0;
  616. params->flickerControl.disabled = 1;
  617. params->flickerControl.coarseJump =
  618. flicker_jumps[sd->mainsFreq]
  619. [params->sensorFps.baserate]
  620. [params->sensorFps.divisor];
  621. params->flickerControl.allowableOverExposure =
  622. find_over_exposure(params->colourParams.brightness);
  623. params->yuvThreshold.yThreshold = 6; /* From windows driver */
  624. params->yuvThreshold.uvThreshold = 6; /* From windows driver */
  625. params->format.subSample = SUBSAMPLE_420;
  626. params->format.yuvOrder = YUVORDER_YUYV;
  627. params->compression.mode = CPIA_COMPRESSION_AUTO;
  628. params->compression.decimation = NO_DECIMATION;
  629. params->compressionTarget.frTargeting = COMP_TARGET_DEF;
  630. params->compressionTarget.targetFR = 15; /* From windows driver */
  631. params->compressionTarget.targetQ = 5; /* From windows driver */
  632. params->qx3.qx3_detected = 0;
  633. params->qx3.toplight = 0;
  634. params->qx3.bottomlight = 0;
  635. params->qx3.button = 0;
  636. params->qx3.cradled = 0;
  637. }
  638. static void printstatus(struct gspca_dev *gspca_dev, struct cam_params *params)
  639. {
  640. PDEBUG(D_PROBE, "status: %02x %02x %02x %02x %02x %02x %02x %02x",
  641. params->status.systemState, params->status.grabState,
  642. params->status.streamState, params->status.fatalError,
  643. params->status.cmdError, params->status.debugFlags,
  644. params->status.vpStatus, params->status.errorCode);
  645. }
  646. static int goto_low_power(struct gspca_dev *gspca_dev)
  647. {
  648. struct sd *sd = (struct sd *) gspca_dev;
  649. int ret;
  650. ret = do_command(gspca_dev, CPIA_COMMAND_GotoLoPower, 0, 0, 0, 0);
  651. if (ret)
  652. return ret;
  653. ret = do_command(gspca_dev, CPIA_COMMAND_GetCameraStatus, 0, 0, 0, 0);
  654. if (ret)
  655. return ret;
  656. if (sd->params.status.systemState != LO_POWER_STATE) {
  657. if (sd->params.status.systemState != WARM_BOOT_STATE) {
  658. PERR("unexpected state after lo power cmd: %02x",
  659. sd->params.status.systemState);
  660. printstatus(gspca_dev, &sd->params);
  661. }
  662. return -EIO;
  663. }
  664. PDEBUG(D_CONF, "camera now in LOW power state");
  665. return 0;
  666. }
  667. static int goto_high_power(struct gspca_dev *gspca_dev)
  668. {
  669. struct sd *sd = (struct sd *) gspca_dev;
  670. int ret;
  671. ret = do_command(gspca_dev, CPIA_COMMAND_GotoHiPower, 0, 0, 0, 0);
  672. if (ret)
  673. return ret;
  674. msleep_interruptible(40); /* windows driver does it too */
  675. if (signal_pending(current))
  676. return -EINTR;
  677. ret = do_command(gspca_dev, CPIA_COMMAND_GetCameraStatus, 0, 0, 0, 0);
  678. if (ret)
  679. return ret;
  680. if (sd->params.status.systemState != HI_POWER_STATE) {
  681. PERR("unexpected state after hi power cmd: %02x",
  682. sd->params.status.systemState);
  683. printstatus(gspca_dev, &sd->params);
  684. return -EIO;
  685. }
  686. PDEBUG(D_CONF, "camera now in HIGH power state");
  687. return 0;
  688. }
  689. static int get_version_information(struct gspca_dev *gspca_dev)
  690. {
  691. int ret;
  692. /* GetCPIAVersion */
  693. ret = do_command(gspca_dev, CPIA_COMMAND_GetCPIAVersion, 0, 0, 0, 0);
  694. if (ret)
  695. return ret;
  696. /* GetPnPID */
  697. return do_command(gspca_dev, CPIA_COMMAND_GetPnPID, 0, 0, 0, 0);
  698. }
  699. static int save_camera_state(struct gspca_dev *gspca_dev)
  700. {
  701. int ret;
  702. ret = do_command(gspca_dev, CPIA_COMMAND_GetColourBalance, 0, 0, 0, 0);
  703. if (ret)
  704. return ret;
  705. return do_command(gspca_dev, CPIA_COMMAND_GetExposure, 0, 0, 0, 0);
  706. }
  707. static int command_setformat(struct gspca_dev *gspca_dev)
  708. {
  709. struct sd *sd = (struct sd *) gspca_dev;
  710. int ret;
  711. ret = do_command(gspca_dev, CPIA_COMMAND_SetFormat,
  712. sd->params.format.videoSize,
  713. sd->params.format.subSample,
  714. sd->params.format.yuvOrder, 0);
  715. if (ret)
  716. return ret;
  717. return do_command(gspca_dev, CPIA_COMMAND_SetROI,
  718. sd->params.roi.colStart, sd->params.roi.colEnd,
  719. sd->params.roi.rowStart, sd->params.roi.rowEnd);
  720. }
  721. static int command_setcolourparams(struct gspca_dev *gspca_dev)
  722. {
  723. struct sd *sd = (struct sd *) gspca_dev;
  724. return do_command(gspca_dev, CPIA_COMMAND_SetColourParams,
  725. sd->params.colourParams.brightness,
  726. sd->params.colourParams.contrast,
  727. sd->params.colourParams.saturation, 0);
  728. }
  729. static int command_setapcor(struct gspca_dev *gspca_dev)
  730. {
  731. struct sd *sd = (struct sd *) gspca_dev;
  732. return do_command(gspca_dev, CPIA_COMMAND_SetApcor,
  733. sd->params.apcor.gain1,
  734. sd->params.apcor.gain2,
  735. sd->params.apcor.gain4,
  736. sd->params.apcor.gain8);
  737. }
  738. static int command_setvloffset(struct gspca_dev *gspca_dev)
  739. {
  740. struct sd *sd = (struct sd *) gspca_dev;
  741. return do_command(gspca_dev, CPIA_COMMAND_SetVLOffset,
  742. sd->params.vlOffset.gain1,
  743. sd->params.vlOffset.gain2,
  744. sd->params.vlOffset.gain4,
  745. sd->params.vlOffset.gain8);
  746. }
  747. static int command_setexposure(struct gspca_dev *gspca_dev)
  748. {
  749. struct sd *sd = (struct sd *) gspca_dev;
  750. int ret;
  751. ret = do_command_extended(gspca_dev, CPIA_COMMAND_SetExposure,
  752. sd->params.exposure.gainMode,
  753. 1,
  754. sd->params.exposure.compMode,
  755. sd->params.exposure.centreWeight,
  756. sd->params.exposure.gain,
  757. sd->params.exposure.fineExp,
  758. sd->params.exposure.coarseExpLo,
  759. sd->params.exposure.coarseExpHi,
  760. sd->params.exposure.redComp,
  761. sd->params.exposure.green1Comp,
  762. sd->params.exposure.green2Comp,
  763. sd->params.exposure.blueComp);
  764. if (ret)
  765. return ret;
  766. if (sd->params.exposure.expMode != 1) {
  767. ret = do_command_extended(gspca_dev, CPIA_COMMAND_SetExposure,
  768. 0,
  769. sd->params.exposure.expMode,
  770. 0, 0,
  771. sd->params.exposure.gain,
  772. sd->params.exposure.fineExp,
  773. sd->params.exposure.coarseExpLo,
  774. sd->params.exposure.coarseExpHi,
  775. 0, 0, 0, 0);
  776. }
  777. return ret;
  778. }
  779. static int command_setcolourbalance(struct gspca_dev *gspca_dev)
  780. {
  781. struct sd *sd = (struct sd *) gspca_dev;
  782. if (sd->params.colourBalance.balanceMode == 1) {
  783. int ret;
  784. ret = do_command(gspca_dev, CPIA_COMMAND_SetColourBalance,
  785. 1,
  786. sd->params.colourBalance.redGain,
  787. sd->params.colourBalance.greenGain,
  788. sd->params.colourBalance.blueGain);
  789. if (ret)
  790. return ret;
  791. return do_command(gspca_dev, CPIA_COMMAND_SetColourBalance,
  792. 3, 0, 0, 0);
  793. }
  794. if (sd->params.colourBalance.balanceMode == 2) {
  795. return do_command(gspca_dev, CPIA_COMMAND_SetColourBalance,
  796. 2, 0, 0, 0);
  797. }
  798. if (sd->params.colourBalance.balanceMode == 3) {
  799. return do_command(gspca_dev, CPIA_COMMAND_SetColourBalance,
  800. 3, 0, 0, 0);
  801. }
  802. return -EINVAL;
  803. }
  804. static int command_setcompressiontarget(struct gspca_dev *gspca_dev)
  805. {
  806. struct sd *sd = (struct sd *) gspca_dev;
  807. return do_command(gspca_dev, CPIA_COMMAND_SetCompressionTarget,
  808. sd->params.compressionTarget.frTargeting,
  809. sd->params.compressionTarget.targetFR,
  810. sd->params.compressionTarget.targetQ, 0);
  811. }
  812. static int command_setyuvtresh(struct gspca_dev *gspca_dev)
  813. {
  814. struct sd *sd = (struct sd *) gspca_dev;
  815. return do_command(gspca_dev, CPIA_COMMAND_SetYUVThresh,
  816. sd->params.yuvThreshold.yThreshold,
  817. sd->params.yuvThreshold.uvThreshold, 0, 0);
  818. }
  819. static int command_setcompressionparams(struct gspca_dev *gspca_dev)
  820. {
  821. struct sd *sd = (struct sd *) gspca_dev;
  822. return do_command_extended(gspca_dev,
  823. CPIA_COMMAND_SetCompressionParams,
  824. 0, 0, 0, 0,
  825. sd->params.compressionParams.hysteresis,
  826. sd->params.compressionParams.threshMax,
  827. sd->params.compressionParams.smallStep,
  828. sd->params.compressionParams.largeStep,
  829. sd->params.compressionParams.decimationHysteresis,
  830. sd->params.compressionParams.frDiffStepThresh,
  831. sd->params.compressionParams.qDiffStepThresh,
  832. sd->params.compressionParams.decimationThreshMod);
  833. }
  834. static int command_setcompression(struct gspca_dev *gspca_dev)
  835. {
  836. struct sd *sd = (struct sd *) gspca_dev;
  837. return do_command(gspca_dev, CPIA_COMMAND_SetCompression,
  838. sd->params.compression.mode,
  839. sd->params.compression.decimation, 0, 0);
  840. }
  841. static int command_setsensorfps(struct gspca_dev *gspca_dev)
  842. {
  843. struct sd *sd = (struct sd *) gspca_dev;
  844. return do_command(gspca_dev, CPIA_COMMAND_SetSensorFPS,
  845. sd->params.sensorFps.divisor,
  846. sd->params.sensorFps.baserate, 0, 0);
  847. }
  848. static int command_setflickerctrl(struct gspca_dev *gspca_dev)
  849. {
  850. struct sd *sd = (struct sd *) gspca_dev;
  851. return do_command(gspca_dev, CPIA_COMMAND_SetFlickerCtrl,
  852. sd->params.flickerControl.flickerMode,
  853. sd->params.flickerControl.coarseJump,
  854. sd->params.flickerControl.allowableOverExposure,
  855. 0);
  856. }
  857. static int command_setecptiming(struct gspca_dev *gspca_dev)
  858. {
  859. struct sd *sd = (struct sd *) gspca_dev;
  860. return do_command(gspca_dev, CPIA_COMMAND_SetECPTiming,
  861. sd->params.ecpTiming, 0, 0, 0);
  862. }
  863. static int command_pause(struct gspca_dev *gspca_dev)
  864. {
  865. return do_command(gspca_dev, CPIA_COMMAND_EndStreamCap, 0, 0, 0, 0);
  866. }
  867. static int command_resume(struct gspca_dev *gspca_dev)
  868. {
  869. struct sd *sd = (struct sd *) gspca_dev;
  870. return do_command(gspca_dev, CPIA_COMMAND_InitStreamCap,
  871. 0, sd->params.streamStartLine, 0, 0);
  872. }
  873. static int command_setlights(struct gspca_dev *gspca_dev)
  874. {
  875. struct sd *sd = (struct sd *) gspca_dev;
  876. int ret, p1, p2;
  877. p1 = (sd->params.qx3.bottomlight == 0) << 1;
  878. p2 = (sd->params.qx3.toplight == 0) << 3;
  879. ret = do_command(gspca_dev, CPIA_COMMAND_WriteVCReg,
  880. 0x90, 0x8f, 0x50, 0);
  881. if (ret)
  882. return ret;
  883. return do_command(gspca_dev, CPIA_COMMAND_WriteMCPort, 2, 0,
  884. p1 | p2 | 0xe0, 0);
  885. }
  886. static int set_flicker(struct gspca_dev *gspca_dev, int on, int apply)
  887. {
  888. /* Everything in here is from the Windows driver */
  889. /* define for compgain calculation */
  890. #if 0
  891. #define COMPGAIN(base, curexp, newexp) \
  892. (u8) ((((float) base - 128.0) * ((float) curexp / (float) newexp)) + 128.5)
  893. #define EXP_FROM_COMP(basecomp, curcomp, curexp) \
  894. (u16)((float)curexp * (float)(u8)(curcomp + 128) / \
  895. (float)(u8)(basecomp - 128))
  896. #else
  897. /* equivalent functions without floating point math */
  898. #define COMPGAIN(base, curexp, newexp) \
  899. (u8)(128 + (((u32)(2*(base-128)*curexp + newexp)) / (2 * newexp)))
  900. #define EXP_FROM_COMP(basecomp, curcomp, curexp) \
  901. (u16)(((u32)(curexp * (u8)(curcomp + 128)) / (u8)(basecomp - 128)))
  902. #endif
  903. struct sd *sd = (struct sd *) gspca_dev;
  904. int currentexp = sd->params.exposure.coarseExpLo +
  905. sd->params.exposure.coarseExpHi * 256;
  906. int ret, startexp;
  907. if (on) {
  908. int cj = sd->params.flickerControl.coarseJump;
  909. sd->params.flickerControl.flickerMode = 1;
  910. sd->params.flickerControl.disabled = 0;
  911. if (sd->params.exposure.expMode != 2) {
  912. sd->params.exposure.expMode = 2;
  913. sd->exposure_status = EXPOSURE_NORMAL;
  914. }
  915. currentexp = currentexp << sd->params.exposure.gain;
  916. sd->params.exposure.gain = 0;
  917. /* round down current exposure to nearest value */
  918. startexp = (currentexp + ROUND_UP_EXP_FOR_FLICKER) / cj;
  919. if (startexp < 1)
  920. startexp = 1;
  921. startexp = (startexp * cj) - 1;
  922. if (FIRMWARE_VERSION(1, 2))
  923. while (startexp > MAX_EXP_102)
  924. startexp -= cj;
  925. else
  926. while (startexp > MAX_EXP)
  927. startexp -= cj;
  928. sd->params.exposure.coarseExpLo = startexp & 0xff;
  929. sd->params.exposure.coarseExpHi = startexp >> 8;
  930. if (currentexp > startexp) {
  931. if (currentexp > (2 * startexp))
  932. currentexp = 2 * startexp;
  933. sd->params.exposure.redComp =
  934. COMPGAIN(COMP_RED, currentexp, startexp);
  935. sd->params.exposure.green1Comp =
  936. COMPGAIN(COMP_GREEN1, currentexp, startexp);
  937. sd->params.exposure.green2Comp =
  938. COMPGAIN(COMP_GREEN2, currentexp, startexp);
  939. sd->params.exposure.blueComp =
  940. COMPGAIN(COMP_BLUE, currentexp, startexp);
  941. } else {
  942. sd->params.exposure.redComp = COMP_RED;
  943. sd->params.exposure.green1Comp = COMP_GREEN1;
  944. sd->params.exposure.green2Comp = COMP_GREEN2;
  945. sd->params.exposure.blueComp = COMP_BLUE;
  946. }
  947. if (FIRMWARE_VERSION(1, 2))
  948. sd->params.exposure.compMode = 0;
  949. else
  950. sd->params.exposure.compMode = 1;
  951. sd->params.apcor.gain1 = 0x18;
  952. sd->params.apcor.gain2 = 0x18;
  953. sd->params.apcor.gain4 = 0x16;
  954. sd->params.apcor.gain8 = 0x14;
  955. } else {
  956. sd->params.flickerControl.flickerMode = 0;
  957. sd->params.flickerControl.disabled = 1;
  958. /* Average equivalent coarse for each comp channel */
  959. startexp = EXP_FROM_COMP(COMP_RED,
  960. sd->params.exposure.redComp, currentexp);
  961. startexp += EXP_FROM_COMP(COMP_GREEN1,
  962. sd->params.exposure.green1Comp, currentexp);
  963. startexp += EXP_FROM_COMP(COMP_GREEN2,
  964. sd->params.exposure.green2Comp, currentexp);
  965. startexp += EXP_FROM_COMP(COMP_BLUE,
  966. sd->params.exposure.blueComp, currentexp);
  967. startexp = startexp >> 2;
  968. while (startexp > MAX_EXP && sd->params.exposure.gain <
  969. sd->params.exposure.gainMode - 1) {
  970. startexp = startexp >> 1;
  971. ++sd->params.exposure.gain;
  972. }
  973. if (FIRMWARE_VERSION(1, 2) && startexp > MAX_EXP_102)
  974. startexp = MAX_EXP_102;
  975. if (startexp > MAX_EXP)
  976. startexp = MAX_EXP;
  977. sd->params.exposure.coarseExpLo = startexp & 0xff;
  978. sd->params.exposure.coarseExpHi = startexp >> 8;
  979. sd->params.exposure.redComp = COMP_RED;
  980. sd->params.exposure.green1Comp = COMP_GREEN1;
  981. sd->params.exposure.green2Comp = COMP_GREEN2;
  982. sd->params.exposure.blueComp = COMP_BLUE;
  983. sd->params.exposure.compMode = 1;
  984. sd->params.apcor.gain1 = 0x18;
  985. sd->params.apcor.gain2 = 0x16;
  986. sd->params.apcor.gain4 = 0x24;
  987. sd->params.apcor.gain8 = 0x34;
  988. }
  989. sd->params.vlOffset.gain1 = 20;
  990. sd->params.vlOffset.gain2 = 24;
  991. sd->params.vlOffset.gain4 = 26;
  992. sd->params.vlOffset.gain8 = 26;
  993. if (apply) {
  994. ret = command_setexposure(gspca_dev);
  995. if (ret)
  996. return ret;
  997. ret = command_setapcor(gspca_dev);
  998. if (ret)
  999. return ret;
  1000. ret = command_setvloffset(gspca_dev);
  1001. if (ret)
  1002. return ret;
  1003. ret = command_setflickerctrl(gspca_dev);
  1004. if (ret)
  1005. return ret;
  1006. }
  1007. return 0;
  1008. #undef EXP_FROM_COMP
  1009. #undef COMPGAIN
  1010. }
  1011. /* monitor the exposure and adjust the sensor frame rate if needed */
  1012. static void monitor_exposure(struct gspca_dev *gspca_dev)
  1013. {
  1014. struct sd *sd = (struct sd *) gspca_dev;
  1015. u8 exp_acc, bcomp, cmd[8];
  1016. int ret, light_exp, dark_exp, very_dark_exp;
  1017. int old_exposure, new_exposure, framerate;
  1018. int setfps = 0, setexp = 0, setflicker = 0;
  1019. /* get necessary stats and register settings from camera */
  1020. /* do_command can't handle this, so do it ourselves */
  1021. cmd[0] = CPIA_COMMAND_ReadVPRegs >> 8;
  1022. cmd[1] = CPIA_COMMAND_ReadVPRegs & 0xff;
  1023. cmd[2] = 30;
  1024. cmd[3] = 4;
  1025. cmd[4] = 9;
  1026. cmd[5] = 8;
  1027. cmd[6] = 8;
  1028. cmd[7] = 0;
  1029. ret = cpia_usb_transferCmd(gspca_dev, cmd);
  1030. if (ret) {
  1031. pr_err("ReadVPRegs(30,4,9,8) - failed: %d\n", ret);
  1032. return;
  1033. }
  1034. exp_acc = gspca_dev->usb_buf[0];
  1035. bcomp = gspca_dev->usb_buf[1];
  1036. light_exp = sd->params.colourParams.brightness +
  1037. TC - 50 + EXP_ACC_LIGHT;
  1038. if (light_exp > 255)
  1039. light_exp = 255;
  1040. dark_exp = sd->params.colourParams.brightness +
  1041. TC - 50 - EXP_ACC_DARK;
  1042. if (dark_exp < 0)
  1043. dark_exp = 0;
  1044. very_dark_exp = dark_exp / 2;
  1045. old_exposure = sd->params.exposure.coarseExpHi * 256 +
  1046. sd->params.exposure.coarseExpLo;
  1047. if (!sd->params.flickerControl.disabled) {
  1048. /* Flicker control on */
  1049. int max_comp = FIRMWARE_VERSION(1, 2) ? MAX_COMP :
  1050. HIGH_COMP_102;
  1051. bcomp += 128; /* decode */
  1052. if (bcomp >= max_comp && exp_acc < dark_exp) {
  1053. /* dark */
  1054. if (exp_acc < very_dark_exp) {
  1055. /* very dark */
  1056. if (sd->exposure_status == EXPOSURE_VERY_DARK)
  1057. ++sd->exposure_count;
  1058. else {
  1059. sd->exposure_status =
  1060. EXPOSURE_VERY_DARK;
  1061. sd->exposure_count = 1;
  1062. }
  1063. } else {
  1064. /* just dark */
  1065. if (sd->exposure_status == EXPOSURE_DARK)
  1066. ++sd->exposure_count;
  1067. else {
  1068. sd->exposure_status = EXPOSURE_DARK;
  1069. sd->exposure_count = 1;
  1070. }
  1071. }
  1072. } else if (old_exposure <= LOW_EXP || exp_acc > light_exp) {
  1073. /* light */
  1074. if (old_exposure <= VERY_LOW_EXP) {
  1075. /* very light */
  1076. if (sd->exposure_status == EXPOSURE_VERY_LIGHT)
  1077. ++sd->exposure_count;
  1078. else {
  1079. sd->exposure_status =
  1080. EXPOSURE_VERY_LIGHT;
  1081. sd->exposure_count = 1;
  1082. }
  1083. } else {
  1084. /* just light */
  1085. if (sd->exposure_status == EXPOSURE_LIGHT)
  1086. ++sd->exposure_count;
  1087. else {
  1088. sd->exposure_status = EXPOSURE_LIGHT;
  1089. sd->exposure_count = 1;
  1090. }
  1091. }
  1092. } else {
  1093. /* not dark or light */
  1094. sd->exposure_status = EXPOSURE_NORMAL;
  1095. }
  1096. } else {
  1097. /* Flicker control off */
  1098. if (old_exposure >= MAX_EXP && exp_acc < dark_exp) {
  1099. /* dark */
  1100. if (exp_acc < very_dark_exp) {
  1101. /* very dark */
  1102. if (sd->exposure_status == EXPOSURE_VERY_DARK)
  1103. ++sd->exposure_count;
  1104. else {
  1105. sd->exposure_status =
  1106. EXPOSURE_VERY_DARK;
  1107. sd->exposure_count = 1;
  1108. }
  1109. } else {
  1110. /* just dark */
  1111. if (sd->exposure_status == EXPOSURE_DARK)
  1112. ++sd->exposure_count;
  1113. else {
  1114. sd->exposure_status = EXPOSURE_DARK;
  1115. sd->exposure_count = 1;
  1116. }
  1117. }
  1118. } else if (old_exposure <= LOW_EXP || exp_acc > light_exp) {
  1119. /* light */
  1120. if (old_exposure <= VERY_LOW_EXP) {
  1121. /* very light */
  1122. if (sd->exposure_status == EXPOSURE_VERY_LIGHT)
  1123. ++sd->exposure_count;
  1124. else {
  1125. sd->exposure_status =
  1126. EXPOSURE_VERY_LIGHT;
  1127. sd->exposure_count = 1;
  1128. }
  1129. } else {
  1130. /* just light */
  1131. if (sd->exposure_status == EXPOSURE_LIGHT)
  1132. ++sd->exposure_count;
  1133. else {
  1134. sd->exposure_status = EXPOSURE_LIGHT;
  1135. sd->exposure_count = 1;
  1136. }
  1137. }
  1138. } else {
  1139. /* not dark or light */
  1140. sd->exposure_status = EXPOSURE_NORMAL;
  1141. }
  1142. }
  1143. framerate = atomic_read(&sd->fps);
  1144. if (framerate > 30 || framerate < 1)
  1145. framerate = 1;
  1146. if (!sd->params.flickerControl.disabled) {
  1147. /* Flicker control on */
  1148. if ((sd->exposure_status == EXPOSURE_VERY_DARK ||
  1149. sd->exposure_status == EXPOSURE_DARK) &&
  1150. sd->exposure_count >= DARK_TIME * framerate &&
  1151. sd->params.sensorFps.divisor < 2) {
  1152. /* dark for too long */
  1153. ++sd->params.sensorFps.divisor;
  1154. setfps = 1;
  1155. sd->params.flickerControl.coarseJump =
  1156. flicker_jumps[sd->mainsFreq]
  1157. [sd->params.sensorFps.baserate]
  1158. [sd->params.sensorFps.divisor];
  1159. setflicker = 1;
  1160. new_exposure = sd->params.flickerControl.coarseJump-1;
  1161. while (new_exposure < old_exposure / 2)
  1162. new_exposure +=
  1163. sd->params.flickerControl.coarseJump;
  1164. sd->params.exposure.coarseExpLo = new_exposure & 0xff;
  1165. sd->params.exposure.coarseExpHi = new_exposure >> 8;
  1166. setexp = 1;
  1167. sd->exposure_status = EXPOSURE_NORMAL;
  1168. PDEBUG(D_CONF, "Automatically decreasing sensor_fps");
  1169. } else if ((sd->exposure_status == EXPOSURE_VERY_LIGHT ||
  1170. sd->exposure_status == EXPOSURE_LIGHT) &&
  1171. sd->exposure_count >= LIGHT_TIME * framerate &&
  1172. sd->params.sensorFps.divisor > 0) {
  1173. /* light for too long */
  1174. int max_exp = FIRMWARE_VERSION(1, 2) ? MAX_EXP_102 :
  1175. MAX_EXP;
  1176. --sd->params.sensorFps.divisor;
  1177. setfps = 1;
  1178. sd->params.flickerControl.coarseJump =
  1179. flicker_jumps[sd->mainsFreq]
  1180. [sd->params.sensorFps.baserate]
  1181. [sd->params.sensorFps.divisor];
  1182. setflicker = 1;
  1183. new_exposure = sd->params.flickerControl.coarseJump-1;
  1184. while (new_exposure < 2 * old_exposure &&
  1185. new_exposure +
  1186. sd->params.flickerControl.coarseJump < max_exp)
  1187. new_exposure +=
  1188. sd->params.flickerControl.coarseJump;
  1189. sd->params.exposure.coarseExpLo = new_exposure & 0xff;
  1190. sd->params.exposure.coarseExpHi = new_exposure >> 8;
  1191. setexp = 1;
  1192. sd->exposure_status = EXPOSURE_NORMAL;
  1193. PDEBUG(D_CONF, "Automatically increasing sensor_fps");
  1194. }
  1195. } else {
  1196. /* Flicker control off */
  1197. if ((sd->exposure_status == EXPOSURE_VERY_DARK ||
  1198. sd->exposure_status == EXPOSURE_DARK) &&
  1199. sd->exposure_count >= DARK_TIME * framerate &&
  1200. sd->params.sensorFps.divisor < 2) {
  1201. /* dark for too long */
  1202. ++sd->params.sensorFps.divisor;
  1203. setfps = 1;
  1204. if (sd->params.exposure.gain > 0) {
  1205. --sd->params.exposure.gain;
  1206. setexp = 1;
  1207. }
  1208. sd->exposure_status = EXPOSURE_NORMAL;
  1209. PDEBUG(D_CONF, "Automatically decreasing sensor_fps");
  1210. } else if ((sd->exposure_status == EXPOSURE_VERY_LIGHT ||
  1211. sd->exposure_status == EXPOSURE_LIGHT) &&
  1212. sd->exposure_count >= LIGHT_TIME * framerate &&
  1213. sd->params.sensorFps.divisor > 0) {
  1214. /* light for too long */
  1215. --sd->params.sensorFps.divisor;
  1216. setfps = 1;
  1217. if (sd->params.exposure.gain <
  1218. sd->params.exposure.gainMode - 1) {
  1219. ++sd->params.exposure.gain;
  1220. setexp = 1;
  1221. }
  1222. sd->exposure_status = EXPOSURE_NORMAL;
  1223. PDEBUG(D_CONF, "Automatically increasing sensor_fps");
  1224. }
  1225. }
  1226. if (setexp)
  1227. command_setexposure(gspca_dev);
  1228. if (setfps)
  1229. command_setsensorfps(gspca_dev);
  1230. if (setflicker)
  1231. command_setflickerctrl(gspca_dev);
  1232. }
  1233. /*-----------------------------------------------------------------*/
  1234. /* if flicker is switched off, this function switches it back on.It checks,
  1235. however, that conditions are suitable before restarting it.
  1236. This should only be called for firmware version 1.2.
  1237. It also adjust the colour balance when an exposure step is detected - as
  1238. long as flicker is running
  1239. */
  1240. static void restart_flicker(struct gspca_dev *gspca_dev)
  1241. {
  1242. struct sd *sd = (struct sd *) gspca_dev;
  1243. int cam_exposure, old_exp;
  1244. if (!FIRMWARE_VERSION(1, 2))
  1245. return;
  1246. cam_exposure = atomic_read(&sd->cam_exposure);
  1247. if (sd->params.flickerControl.flickerMode == 0 ||
  1248. cam_exposure == 0)
  1249. return;
  1250. old_exp = sd->params.exposure.coarseExpLo +
  1251. sd->params.exposure.coarseExpHi*256;
  1252. /*
  1253. see how far away camera exposure is from a valid
  1254. flicker exposure value
  1255. */
  1256. cam_exposure %= sd->params.flickerControl.coarseJump;
  1257. if (!sd->params.flickerControl.disabled &&
  1258. cam_exposure <= sd->params.flickerControl.coarseJump - 3) {
  1259. /* Flicker control auto-disabled */
  1260. sd->params.flickerControl.disabled = 1;
  1261. }
  1262. if (sd->params.flickerControl.disabled &&
  1263. old_exp > sd->params.flickerControl.coarseJump +
  1264. ROUND_UP_EXP_FOR_FLICKER) {
  1265. /* exposure is now high enough to switch
  1266. flicker control back on */
  1267. set_flicker(gspca_dev, 1, 1);
  1268. }
  1269. }
  1270. /* this function is called at probe time */
  1271. static int sd_config(struct gspca_dev *gspca_dev,
  1272. const struct usb_device_id *id)
  1273. {
  1274. struct sd *sd = (struct sd *) gspca_dev;
  1275. struct cam *cam;
  1276. sd->mainsFreq = FREQ_DEF == V4L2_CID_POWER_LINE_FREQUENCY_60HZ;
  1277. reset_camera_params(gspca_dev);
  1278. PDEBUG(D_PROBE, "cpia CPiA camera detected (vid/pid 0x%04X:0x%04X)",
  1279. id->idVendor, id->idProduct);
  1280. cam = &gspca_dev->cam;
  1281. cam->cam_mode = mode;
  1282. cam->nmodes = ARRAY_SIZE(mode);
  1283. goto_low_power(gspca_dev);
  1284. /* Check the firmware version. */
  1285. sd->params.version.firmwareVersion = 0;
  1286. get_version_information(gspca_dev);
  1287. if (sd->params.version.firmwareVersion != 1) {
  1288. PERR("only firmware version 1 is supported (got: %d)",
  1289. sd->params.version.firmwareVersion);
  1290. return -ENODEV;
  1291. }
  1292. /* A bug in firmware 1-02 limits gainMode to 2 */
  1293. if (sd->params.version.firmwareRevision <= 2 &&
  1294. sd->params.exposure.gainMode > 2) {
  1295. sd->params.exposure.gainMode = 2;
  1296. }
  1297. /* set QX3 detected flag */
  1298. sd->params.qx3.qx3_detected = (sd->params.pnpID.vendor == 0x0813 &&
  1299. sd->params.pnpID.product == 0x0001);
  1300. return 0;
  1301. }
  1302. /* -- start the camera -- */
  1303. static int sd_start(struct gspca_dev *gspca_dev)
  1304. {
  1305. struct sd *sd = (struct sd *) gspca_dev;
  1306. int priv, ret;
  1307. /* Start the camera in low power mode */
  1308. if (goto_low_power(gspca_dev)) {
  1309. if (sd->params.status.systemState != WARM_BOOT_STATE) {
  1310. PERR("unexpected systemstate: %02x",
  1311. sd->params.status.systemState);
  1312. printstatus(gspca_dev, &sd->params);
  1313. return -ENODEV;
  1314. }
  1315. /* FIXME: this is just dirty trial and error */
  1316. ret = goto_high_power(gspca_dev);
  1317. if (ret)
  1318. return ret;
  1319. ret = do_command(gspca_dev, CPIA_COMMAND_DiscardFrame,
  1320. 0, 0, 0, 0);
  1321. if (ret)
  1322. return ret;
  1323. ret = goto_low_power(gspca_dev);
  1324. if (ret)
  1325. return ret;
  1326. }
  1327. /* procedure described in developer's guide p3-28 */
  1328. /* Check the firmware version. */
  1329. sd->params.version.firmwareVersion = 0;
  1330. get_version_information(gspca_dev);
  1331. /* The fatal error checking should be done after
  1332. * the camera powers up (developer's guide p 3-38) */
  1333. /* Set streamState before transition to high power to avoid bug
  1334. * in firmware 1-02 */
  1335. ret = do_command(gspca_dev, CPIA_COMMAND_ModifyCameraStatus,
  1336. STREAMSTATE, 0, STREAM_NOT_READY, 0);
  1337. if (ret)
  1338. return ret;
  1339. /* GotoHiPower */
  1340. ret = goto_high_power(gspca_dev);
  1341. if (ret)
  1342. return ret;
  1343. /* Check the camera status */
  1344. ret = do_command(gspca_dev, CPIA_COMMAND_GetCameraStatus, 0, 0, 0, 0);
  1345. if (ret)
  1346. return ret;
  1347. if (sd->params.status.fatalError) {
  1348. PERR("fatal_error: %04x, vp_status: %04x",
  1349. sd->params.status.fatalError, sd->params.status.vpStatus);
  1350. return -EIO;
  1351. }
  1352. /* VPVersion can't be retrieved before the camera is in HiPower,
  1353. * so get it here instead of in get_version_information. */
  1354. ret = do_command(gspca_dev, CPIA_COMMAND_GetVPVersion, 0, 0, 0, 0);
  1355. if (ret)
  1356. return ret;
  1357. /* Determine video mode settings */
  1358. sd->params.streamStartLine = 120;
  1359. priv = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  1360. if (priv & 0x01) { /* crop */
  1361. sd->params.roi.colStart = 2;
  1362. sd->params.roi.rowStart = 6;
  1363. } else {
  1364. sd->params.roi.colStart = 0;
  1365. sd->params.roi.rowStart = 0;
  1366. }
  1367. if (priv & 0x02) { /* quarter */
  1368. sd->params.format.videoSize = VIDEOSIZE_QCIF;
  1369. sd->params.roi.colStart /= 2;
  1370. sd->params.roi.rowStart /= 2;
  1371. sd->params.streamStartLine /= 2;
  1372. } else
  1373. sd->params.format.videoSize = VIDEOSIZE_CIF;
  1374. sd->params.roi.colEnd = sd->params.roi.colStart +
  1375. (gspca_dev->pixfmt.width >> 3);
  1376. sd->params.roi.rowEnd = sd->params.roi.rowStart +
  1377. (gspca_dev->pixfmt.height >> 2);
  1378. /* And now set the camera to a known state */
  1379. ret = do_command(gspca_dev, CPIA_COMMAND_SetGrabMode,
  1380. CPIA_GRAB_CONTINEOUS, 0, 0, 0);
  1381. if (ret)
  1382. return ret;
  1383. /* We start with compression disabled, as we need one uncompressed
  1384. frame to handle later compressed frames */
  1385. ret = do_command(gspca_dev, CPIA_COMMAND_SetCompression,
  1386. CPIA_COMPRESSION_NONE,
  1387. NO_DECIMATION, 0, 0);
  1388. if (ret)
  1389. return ret;
  1390. ret = command_setcompressiontarget(gspca_dev);
  1391. if (ret)
  1392. return ret;
  1393. ret = command_setcolourparams(gspca_dev);
  1394. if (ret)
  1395. return ret;
  1396. ret = command_setformat(gspca_dev);
  1397. if (ret)
  1398. return ret;
  1399. ret = command_setyuvtresh(gspca_dev);
  1400. if (ret)
  1401. return ret;
  1402. ret = command_setecptiming(gspca_dev);
  1403. if (ret)
  1404. return ret;
  1405. ret = command_setcompressionparams(gspca_dev);
  1406. if (ret)
  1407. return ret;
  1408. ret = command_setexposure(gspca_dev);
  1409. if (ret)
  1410. return ret;
  1411. ret = command_setcolourbalance(gspca_dev);
  1412. if (ret)
  1413. return ret;
  1414. ret = command_setsensorfps(gspca_dev);
  1415. if (ret)
  1416. return ret;
  1417. ret = command_setapcor(gspca_dev);
  1418. if (ret)
  1419. return ret;
  1420. ret = command_setflickerctrl(gspca_dev);
  1421. if (ret)
  1422. return ret;
  1423. ret = command_setvloffset(gspca_dev);
  1424. if (ret)
  1425. return ret;
  1426. /* Start stream */
  1427. ret = command_resume(gspca_dev);
  1428. if (ret)
  1429. return ret;
  1430. /* Wait 6 frames before turning compression on for the sensor to get
  1431. all settings and AEC/ACB to settle */
  1432. sd->first_frame = 6;
  1433. sd->exposure_status = EXPOSURE_NORMAL;
  1434. sd->exposure_count = 0;
  1435. atomic_set(&sd->cam_exposure, 0);
  1436. atomic_set(&sd->fps, 0);
  1437. return 0;
  1438. }
  1439. static void sd_stopN(struct gspca_dev *gspca_dev)
  1440. {
  1441. struct sd *sd __maybe_unused = (struct sd *) gspca_dev;
  1442. command_pause(gspca_dev);
  1443. /* save camera state for later open (developers guide ch 3.5.3) */
  1444. save_camera_state(gspca_dev);
  1445. /* GotoLoPower */
  1446. goto_low_power(gspca_dev);
  1447. /* Update the camera status */
  1448. do_command(gspca_dev, CPIA_COMMAND_GetCameraStatus, 0, 0, 0, 0);
  1449. #if IS_ENABLED(CONFIG_INPUT)
  1450. /* If the last button state is pressed, release it now! */
  1451. if (sd->params.qx3.button) {
  1452. /* The camera latch will hold the pressed state until we reset
  1453. the latch, so we do not reset sd->params.qx3.button now, to
  1454. avoid a false keypress being reported the next sd_start */
  1455. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
  1456. input_sync(gspca_dev->input_dev);
  1457. }
  1458. #endif
  1459. }
  1460. /* this function is called at probe and resume time */
  1461. static int sd_init(struct gspca_dev *gspca_dev)
  1462. {
  1463. struct sd *sd = (struct sd *) gspca_dev;
  1464. int ret;
  1465. /* Start / Stop the camera to make sure we are talking to
  1466. a supported camera, and to get some information from it
  1467. to print. */
  1468. ret = sd_start(gspca_dev);
  1469. if (ret)
  1470. return ret;
  1471. /* Ensure the QX3 illuminators' states are restored upon resume,
  1472. or disable the illuminator controls, if this isn't a QX3 */
  1473. if (sd->params.qx3.qx3_detected)
  1474. command_setlights(gspca_dev);
  1475. sd_stopN(gspca_dev);
  1476. PDEBUG(D_PROBE, "CPIA Version: %d.%02d (%d.%d)",
  1477. sd->params.version.firmwareVersion,
  1478. sd->params.version.firmwareRevision,
  1479. sd->params.version.vcVersion,
  1480. sd->params.version.vcRevision);
  1481. PDEBUG(D_PROBE, "CPIA PnP-ID: %04x:%04x:%04x",
  1482. sd->params.pnpID.vendor, sd->params.pnpID.product,
  1483. sd->params.pnpID.deviceRevision);
  1484. PDEBUG(D_PROBE, "VP-Version: %d.%d %04x",
  1485. sd->params.vpVersion.vpVersion,
  1486. sd->params.vpVersion.vpRevision,
  1487. sd->params.vpVersion.cameraHeadID);
  1488. return 0;
  1489. }
  1490. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  1491. u8 *data,
  1492. int len)
  1493. {
  1494. struct sd *sd = (struct sd *) gspca_dev;
  1495. /* Check for SOF */
  1496. if (len >= 64 &&
  1497. data[0] == MAGIC_0 && data[1] == MAGIC_1 &&
  1498. data[16] == sd->params.format.videoSize &&
  1499. data[17] == sd->params.format.subSample &&
  1500. data[18] == sd->params.format.yuvOrder &&
  1501. data[24] == sd->params.roi.colStart &&
  1502. data[25] == sd->params.roi.colEnd &&
  1503. data[26] == sd->params.roi.rowStart &&
  1504. data[27] == sd->params.roi.rowEnd) {
  1505. u8 *image;
  1506. atomic_set(&sd->cam_exposure, data[39] * 2);
  1507. atomic_set(&sd->fps, data[41]);
  1508. /* Check for proper EOF for last frame */
  1509. image = gspca_dev->image;
  1510. if (image != NULL &&
  1511. gspca_dev->image_len > 4 &&
  1512. image[gspca_dev->image_len - 4] == 0xff &&
  1513. image[gspca_dev->image_len - 3] == 0xff &&
  1514. image[gspca_dev->image_len - 2] == 0xff &&
  1515. image[gspca_dev->image_len - 1] == 0xff)
  1516. gspca_frame_add(gspca_dev, LAST_PACKET,
  1517. NULL, 0);
  1518. gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
  1519. return;
  1520. }
  1521. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  1522. }
  1523. static void sd_dq_callback(struct gspca_dev *gspca_dev)
  1524. {
  1525. struct sd *sd = (struct sd *) gspca_dev;
  1526. /* Set the normal compression settings once we have captured a
  1527. few uncompressed frames (and AEC has hopefully settled) */
  1528. if (sd->first_frame) {
  1529. sd->first_frame--;
  1530. if (sd->first_frame == 0)
  1531. command_setcompression(gspca_dev);
  1532. }
  1533. /* Switch flicker control back on if it got turned off */
  1534. restart_flicker(gspca_dev);
  1535. /* If AEC is enabled, monitor the exposure and
  1536. adjust the sensor frame rate if needed */
  1537. if (sd->params.exposure.expMode == 2)
  1538. monitor_exposure(gspca_dev);
  1539. /* Update our knowledge of the camera state */
  1540. do_command(gspca_dev, CPIA_COMMAND_GetExposure, 0, 0, 0, 0);
  1541. do_command(gspca_dev, CPIA_COMMAND_ReadMCPorts, 0, 0, 0, 0);
  1542. }
  1543. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  1544. {
  1545. struct gspca_dev *gspca_dev =
  1546. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  1547. struct sd *sd = (struct sd *)gspca_dev;
  1548. gspca_dev->usb_err = 0;
  1549. if (!gspca_dev->streaming && ctrl->id != V4L2_CID_POWER_LINE_FREQUENCY)
  1550. return 0;
  1551. switch (ctrl->id) {
  1552. case V4L2_CID_BRIGHTNESS:
  1553. sd->params.colourParams.brightness = ctrl->val;
  1554. sd->params.flickerControl.allowableOverExposure =
  1555. find_over_exposure(sd->params.colourParams.brightness);
  1556. gspca_dev->usb_err = command_setcolourparams(gspca_dev);
  1557. if (!gspca_dev->usb_err)
  1558. gspca_dev->usb_err = command_setflickerctrl(gspca_dev);
  1559. break;
  1560. case V4L2_CID_CONTRAST:
  1561. sd->params.colourParams.contrast = ctrl->val;
  1562. gspca_dev->usb_err = command_setcolourparams(gspca_dev);
  1563. break;
  1564. case V4L2_CID_SATURATION:
  1565. sd->params.colourParams.saturation = ctrl->val;
  1566. gspca_dev->usb_err = command_setcolourparams(gspca_dev);
  1567. break;
  1568. case V4L2_CID_POWER_LINE_FREQUENCY:
  1569. sd->mainsFreq = ctrl->val == V4L2_CID_POWER_LINE_FREQUENCY_60HZ;
  1570. sd->params.flickerControl.coarseJump =
  1571. flicker_jumps[sd->mainsFreq]
  1572. [sd->params.sensorFps.baserate]
  1573. [sd->params.sensorFps.divisor];
  1574. gspca_dev->usb_err = set_flicker(gspca_dev,
  1575. ctrl->val != V4L2_CID_POWER_LINE_FREQUENCY_DISABLED,
  1576. gspca_dev->streaming);
  1577. break;
  1578. case V4L2_CID_ILLUMINATORS_1:
  1579. sd->params.qx3.bottomlight = ctrl->val;
  1580. gspca_dev->usb_err = command_setlights(gspca_dev);
  1581. break;
  1582. case V4L2_CID_ILLUMINATORS_2:
  1583. sd->params.qx3.toplight = ctrl->val;
  1584. gspca_dev->usb_err = command_setlights(gspca_dev);
  1585. break;
  1586. case CPIA1_CID_COMP_TARGET:
  1587. sd->params.compressionTarget.frTargeting = ctrl->val;
  1588. gspca_dev->usb_err = command_setcompressiontarget(gspca_dev);
  1589. break;
  1590. }
  1591. return gspca_dev->usb_err;
  1592. }
  1593. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  1594. .s_ctrl = sd_s_ctrl,
  1595. };
  1596. static int sd_init_controls(struct gspca_dev *gspca_dev)
  1597. {
  1598. struct sd *sd = (struct sd *)gspca_dev;
  1599. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  1600. static const char * const comp_target_menu[] = {
  1601. "Quality",
  1602. "Framerate",
  1603. NULL
  1604. };
  1605. static const struct v4l2_ctrl_config comp_target = {
  1606. .ops = &sd_ctrl_ops,
  1607. .id = CPIA1_CID_COMP_TARGET,
  1608. .type = V4L2_CTRL_TYPE_MENU,
  1609. .name = "Compression Target",
  1610. .qmenu = comp_target_menu,
  1611. .max = 1,
  1612. .def = COMP_TARGET_DEF,
  1613. };
  1614. gspca_dev->vdev.ctrl_handler = hdl;
  1615. v4l2_ctrl_handler_init(hdl, 7);
  1616. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1617. V4L2_CID_BRIGHTNESS, 0, 100, 1, BRIGHTNESS_DEF);
  1618. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1619. V4L2_CID_CONTRAST, 0, 96, 8, CONTRAST_DEF);
  1620. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1621. V4L2_CID_SATURATION, 0, 100, 1, SATURATION_DEF);
  1622. sd->freq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
  1623. V4L2_CID_POWER_LINE_FREQUENCY,
  1624. V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0,
  1625. FREQ_DEF);
  1626. if (sd->params.qx3.qx3_detected) {
  1627. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1628. V4L2_CID_ILLUMINATORS_1, 0, 1, 1,
  1629. ILLUMINATORS_1_DEF);
  1630. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1631. V4L2_CID_ILLUMINATORS_2, 0, 1, 1,
  1632. ILLUMINATORS_2_DEF);
  1633. }
  1634. v4l2_ctrl_new_custom(hdl, &comp_target, NULL);
  1635. if (hdl->error) {
  1636. pr_err("Could not initialize controls\n");
  1637. return hdl->error;
  1638. }
  1639. return 0;
  1640. }
  1641. /* sub-driver description */
  1642. static const struct sd_desc sd_desc = {
  1643. .name = MODULE_NAME,
  1644. .config = sd_config,
  1645. .init = sd_init,
  1646. .init_controls = sd_init_controls,
  1647. .start = sd_start,
  1648. .stopN = sd_stopN,
  1649. .dq_callback = sd_dq_callback,
  1650. .pkt_scan = sd_pkt_scan,
  1651. #if IS_ENABLED(CONFIG_INPUT)
  1652. .other_input = 1,
  1653. #endif
  1654. };
  1655. /* -- module initialisation -- */
  1656. static const struct usb_device_id device_table[] = {
  1657. {USB_DEVICE(0x0553, 0x0002)},
  1658. {USB_DEVICE(0x0813, 0x0001)},
  1659. {}
  1660. };
  1661. MODULE_DEVICE_TABLE(usb, device_table);
  1662. /* -- device connect -- */
  1663. static int sd_probe(struct usb_interface *intf,
  1664. const struct usb_device_id *id)
  1665. {
  1666. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  1667. THIS_MODULE);
  1668. }
  1669. static struct usb_driver sd_driver = {
  1670. .name = MODULE_NAME,
  1671. .id_table = device_table,
  1672. .probe = sd_probe,
  1673. .disconnect = gspca_disconnect,
  1674. #ifdef CONFIG_PM
  1675. .suspend = gspca_suspend,
  1676. .resume = gspca_resume,
  1677. .reset_resume = gspca_resume,
  1678. #endif
  1679. };
  1680. module_usb_driver(sd_driver);