sn9c20x.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421
  1. /*
  2. * Sonix sn9c201 sn9c202 library
  3. *
  4. * Copyright (C) 2012 Jean-Francois Moine <http://moinejf.free.fr>
  5. * Copyright (C) 2008-2009 microdia project <microdia@googlegroups.com>
  6. * Copyright (C) 2009 Brian Johnson <brijohn@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/input.h>
  24. #include "gspca.h"
  25. #include "jpeg.h"
  26. #include <media/v4l2-chip-ident.h>
  27. #include <linux/dmi.h>
  28. MODULE_AUTHOR("Brian Johnson <brijohn@gmail.com>, "
  29. "microdia project <microdia@googlegroups.com>");
  30. MODULE_DESCRIPTION("GSPCA/SN9C20X USB Camera Driver");
  31. MODULE_LICENSE("GPL");
  32. /*
  33. * Pixel format private data
  34. */
  35. #define SCALE_MASK 0x0f
  36. #define SCALE_160x120 0
  37. #define SCALE_320x240 1
  38. #define SCALE_640x480 2
  39. #define SCALE_1280x1024 3
  40. #define MODE_RAW 0x10
  41. #define MODE_JPEG 0x20
  42. #define MODE_SXGA 0x80
  43. #define SENSOR_OV9650 0
  44. #define SENSOR_OV9655 1
  45. #define SENSOR_SOI968 2
  46. #define SENSOR_OV7660 3
  47. #define SENSOR_OV7670 4
  48. #define SENSOR_MT9V011 5
  49. #define SENSOR_MT9V111 6
  50. #define SENSOR_MT9V112 7
  51. #define SENSOR_MT9M001 8
  52. #define SENSOR_MT9M111 9
  53. #define SENSOR_MT9M112 10
  54. #define SENSOR_HV7131R 11
  55. #define SENSOR_MT9VPRB 12
  56. /* camera flags */
  57. #define HAS_NO_BUTTON 0x1
  58. #define LED_REVERSE 0x2 /* some cameras unset gpio to turn on leds */
  59. #define FLIP_DETECT 0x4
  60. /* specific webcam descriptor */
  61. struct sd {
  62. struct gspca_dev gspca_dev;
  63. struct v4l2_ctrl_handler ctrl_handler;
  64. struct { /* color control cluster */
  65. struct v4l2_ctrl *brightness;
  66. struct v4l2_ctrl *contrast;
  67. struct v4l2_ctrl *saturation;
  68. struct v4l2_ctrl *hue;
  69. };
  70. struct { /* blue/red balance control cluster */
  71. struct v4l2_ctrl *blue;
  72. struct v4l2_ctrl *red;
  73. };
  74. struct { /* h/vflip control cluster */
  75. struct v4l2_ctrl *hflip;
  76. struct v4l2_ctrl *vflip;
  77. };
  78. struct v4l2_ctrl *gamma;
  79. struct { /* autogain and exposure or gain control cluster */
  80. struct v4l2_ctrl *autogain;
  81. struct v4l2_ctrl *exposure;
  82. struct v4l2_ctrl *gain;
  83. };
  84. struct v4l2_ctrl *jpegqual;
  85. struct work_struct work;
  86. struct workqueue_struct *work_thread;
  87. u32 pktsz; /* (used by pkt_scan) */
  88. u16 npkt;
  89. s8 nchg;
  90. u8 fmt; /* (used for JPEG QTAB update */
  91. #define MIN_AVG_LUM 80
  92. #define MAX_AVG_LUM 130
  93. atomic_t avg_lum;
  94. u8 old_step;
  95. u8 older_step;
  96. u8 exposure_step;
  97. u8 i2c_addr;
  98. u8 i2c_intf;
  99. u8 sensor;
  100. u8 hstart;
  101. u8 vstart;
  102. u8 jpeg_hdr[JPEG_HDR_SZ];
  103. u8 flags;
  104. };
  105. static void qual_upd(struct work_struct *work);
  106. struct i2c_reg_u8 {
  107. u8 reg;
  108. u8 val;
  109. };
  110. struct i2c_reg_u16 {
  111. u8 reg;
  112. u16 val;
  113. };
  114. static const struct dmi_system_id flip_dmi_table[] = {
  115. {
  116. .ident = "MSI MS-1034",
  117. .matches = {
  118. DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD."),
  119. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1034"),
  120. DMI_MATCH(DMI_PRODUCT_VERSION, "0341")
  121. }
  122. },
  123. {
  124. .ident = "MSI MS-1632",
  125. .matches = {
  126. DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),
  127. DMI_MATCH(DMI_BOARD_NAME, "MS-1632")
  128. }
  129. },
  130. {
  131. .ident = "MSI MS-1633X",
  132. .matches = {
  133. DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),
  134. DMI_MATCH(DMI_BOARD_NAME, "MS-1633X")
  135. }
  136. },
  137. {
  138. .ident = "MSI MS-1635X",
  139. .matches = {
  140. DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),
  141. DMI_MATCH(DMI_BOARD_NAME, "MS-1635X")
  142. }
  143. },
  144. {
  145. .ident = "ASUSTeK W7J",
  146. .matches = {
  147. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc."),
  148. DMI_MATCH(DMI_BOARD_NAME, "W7J ")
  149. }
  150. },
  151. {}
  152. };
  153. static const struct v4l2_pix_format vga_mode[] = {
  154. {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  155. .bytesperline = 160,
  156. .sizeimage = 160 * 120 * 4 / 8 + 590,
  157. .colorspace = V4L2_COLORSPACE_JPEG,
  158. .priv = SCALE_160x120 | MODE_JPEG},
  159. {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  160. .bytesperline = 160,
  161. .sizeimage = 160 * 120,
  162. .colorspace = V4L2_COLORSPACE_SRGB,
  163. .priv = SCALE_160x120 | MODE_RAW},
  164. {160, 120, V4L2_PIX_FMT_SN9C20X_I420, V4L2_FIELD_NONE,
  165. .bytesperline = 160,
  166. .sizeimage = 240 * 120,
  167. .colorspace = V4L2_COLORSPACE_SRGB,
  168. .priv = SCALE_160x120},
  169. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  170. .bytesperline = 320,
  171. .sizeimage = 320 * 240 * 4 / 8 + 590,
  172. .colorspace = V4L2_COLORSPACE_JPEG,
  173. .priv = SCALE_320x240 | MODE_JPEG},
  174. {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  175. .bytesperline = 320,
  176. .sizeimage = 320 * 240 ,
  177. .colorspace = V4L2_COLORSPACE_SRGB,
  178. .priv = SCALE_320x240 | MODE_RAW},
  179. {320, 240, V4L2_PIX_FMT_SN9C20X_I420, V4L2_FIELD_NONE,
  180. .bytesperline = 320,
  181. .sizeimage = 480 * 240 ,
  182. .colorspace = V4L2_COLORSPACE_SRGB,
  183. .priv = SCALE_320x240},
  184. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  185. .bytesperline = 640,
  186. .sizeimage = 640 * 480 * 4 / 8 + 590,
  187. .colorspace = V4L2_COLORSPACE_JPEG,
  188. .priv = SCALE_640x480 | MODE_JPEG},
  189. {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  190. .bytesperline = 640,
  191. .sizeimage = 640 * 480,
  192. .colorspace = V4L2_COLORSPACE_SRGB,
  193. .priv = SCALE_640x480 | MODE_RAW},
  194. {640, 480, V4L2_PIX_FMT_SN9C20X_I420, V4L2_FIELD_NONE,
  195. .bytesperline = 640,
  196. .sizeimage = 960 * 480,
  197. .colorspace = V4L2_COLORSPACE_SRGB,
  198. .priv = SCALE_640x480},
  199. };
  200. static const struct v4l2_pix_format sxga_mode[] = {
  201. {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  202. .bytesperline = 160,
  203. .sizeimage = 160 * 120 * 4 / 8 + 590,
  204. .colorspace = V4L2_COLORSPACE_JPEG,
  205. .priv = SCALE_160x120 | MODE_JPEG},
  206. {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  207. .bytesperline = 160,
  208. .sizeimage = 160 * 120,
  209. .colorspace = V4L2_COLORSPACE_SRGB,
  210. .priv = SCALE_160x120 | MODE_RAW},
  211. {160, 120, V4L2_PIX_FMT_SN9C20X_I420, V4L2_FIELD_NONE,
  212. .bytesperline = 160,
  213. .sizeimage = 240 * 120,
  214. .colorspace = V4L2_COLORSPACE_SRGB,
  215. .priv = SCALE_160x120},
  216. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  217. .bytesperline = 320,
  218. .sizeimage = 320 * 240 * 4 / 8 + 590,
  219. .colorspace = V4L2_COLORSPACE_JPEG,
  220. .priv = SCALE_320x240 | MODE_JPEG},
  221. {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  222. .bytesperline = 320,
  223. .sizeimage = 320 * 240 ,
  224. .colorspace = V4L2_COLORSPACE_SRGB,
  225. .priv = SCALE_320x240 | MODE_RAW},
  226. {320, 240, V4L2_PIX_FMT_SN9C20X_I420, V4L2_FIELD_NONE,
  227. .bytesperline = 320,
  228. .sizeimage = 480 * 240 ,
  229. .colorspace = V4L2_COLORSPACE_SRGB,
  230. .priv = SCALE_320x240},
  231. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  232. .bytesperline = 640,
  233. .sizeimage = 640 * 480 * 4 / 8 + 590,
  234. .colorspace = V4L2_COLORSPACE_JPEG,
  235. .priv = SCALE_640x480 | MODE_JPEG},
  236. {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  237. .bytesperline = 640,
  238. .sizeimage = 640 * 480,
  239. .colorspace = V4L2_COLORSPACE_SRGB,
  240. .priv = SCALE_640x480 | MODE_RAW},
  241. {640, 480, V4L2_PIX_FMT_SN9C20X_I420, V4L2_FIELD_NONE,
  242. .bytesperline = 640,
  243. .sizeimage = 960 * 480,
  244. .colorspace = V4L2_COLORSPACE_SRGB,
  245. .priv = SCALE_640x480},
  246. {1280, 1024, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  247. .bytesperline = 1280,
  248. .sizeimage = 1280 * 1024,
  249. .colorspace = V4L2_COLORSPACE_SRGB,
  250. .priv = SCALE_1280x1024 | MODE_RAW | MODE_SXGA},
  251. };
  252. static const struct v4l2_pix_format mono_mode[] = {
  253. {160, 120, V4L2_PIX_FMT_GREY, V4L2_FIELD_NONE,
  254. .bytesperline = 160,
  255. .sizeimage = 160 * 120,
  256. .colorspace = V4L2_COLORSPACE_SRGB,
  257. .priv = SCALE_160x120 | MODE_RAW},
  258. {320, 240, V4L2_PIX_FMT_GREY, V4L2_FIELD_NONE,
  259. .bytesperline = 320,
  260. .sizeimage = 320 * 240 ,
  261. .colorspace = V4L2_COLORSPACE_SRGB,
  262. .priv = SCALE_320x240 | MODE_RAW},
  263. {640, 480, V4L2_PIX_FMT_GREY, V4L2_FIELD_NONE,
  264. .bytesperline = 640,
  265. .sizeimage = 640 * 480,
  266. .colorspace = V4L2_COLORSPACE_SRGB,
  267. .priv = SCALE_640x480 | MODE_RAW},
  268. {1280, 1024, V4L2_PIX_FMT_GREY, V4L2_FIELD_NONE,
  269. .bytesperline = 1280,
  270. .sizeimage = 1280 * 1024,
  271. .colorspace = V4L2_COLORSPACE_SRGB,
  272. .priv = SCALE_1280x1024 | MODE_RAW | MODE_SXGA},
  273. };
  274. static const s16 hsv_red_x[] = {
  275. 41, 44, 46, 48, 50, 52, 54, 56,
  276. 58, 60, 62, 64, 66, 68, 70, 72,
  277. 74, 76, 78, 80, 81, 83, 85, 87,
  278. 88, 90, 92, 93, 95, 97, 98, 100,
  279. 101, 102, 104, 105, 107, 108, 109, 110,
  280. 112, 113, 114, 115, 116, 117, 118, 119,
  281. 120, 121, 122, 123, 123, 124, 125, 125,
  282. 126, 127, 127, 128, 128, 129, 129, 129,
  283. 130, 130, 130, 130, 131, 131, 131, 131,
  284. 131, 131, 131, 131, 130, 130, 130, 130,
  285. 129, 129, 129, 128, 128, 127, 127, 126,
  286. 125, 125, 124, 123, 122, 122, 121, 120,
  287. 119, 118, 117, 116, 115, 114, 112, 111,
  288. 110, 109, 107, 106, 105, 103, 102, 101,
  289. 99, 98, 96, 94, 93, 91, 90, 88,
  290. 86, 84, 83, 81, 79, 77, 75, 74,
  291. 72, 70, 68, 66, 64, 62, 60, 58,
  292. 56, 54, 52, 49, 47, 45, 43, 41,
  293. 39, 36, 34, 32, 30, 28, 25, 23,
  294. 21, 19, 16, 14, 12, 9, 7, 5,
  295. 3, 0, -1, -3, -6, -8, -10, -12,
  296. -15, -17, -19, -22, -24, -26, -28, -30,
  297. -33, -35, -37, -39, -41, -44, -46, -48,
  298. -50, -52, -54, -56, -58, -60, -62, -64,
  299. -66, -68, -70, -72, -74, -76, -78, -80,
  300. -81, -83, -85, -87, -88, -90, -92, -93,
  301. -95, -97, -98, -100, -101, -102, -104, -105,
  302. -107, -108, -109, -110, -112, -113, -114, -115,
  303. -116, -117, -118, -119, -120, -121, -122, -123,
  304. -123, -124, -125, -125, -126, -127, -127, -128,
  305. -128, -128, -128, -128, -128, -128, -128, -128,
  306. -128, -128, -128, -128, -128, -128, -128, -128,
  307. -128, -128, -128, -128, -128, -128, -128, -128,
  308. -128, -127, -127, -126, -125, -125, -124, -123,
  309. -122, -122, -121, -120, -119, -118, -117, -116,
  310. -115, -114, -112, -111, -110, -109, -107, -106,
  311. -105, -103, -102, -101, -99, -98, -96, -94,
  312. -93, -91, -90, -88, -86, -84, -83, -81,
  313. -79, -77, -75, -74, -72, -70, -68, -66,
  314. -64, -62, -60, -58, -56, -54, -52, -49,
  315. -47, -45, -43, -41, -39, -36, -34, -32,
  316. -30, -28, -25, -23, -21, -19, -16, -14,
  317. -12, -9, -7, -5, -3, 0, 1, 3,
  318. 6, 8, 10, 12, 15, 17, 19, 22,
  319. 24, 26, 28, 30, 33, 35, 37, 39, 41
  320. };
  321. static const s16 hsv_red_y[] = {
  322. 82, 80, 78, 76, 74, 73, 71, 69,
  323. 67, 65, 63, 61, 58, 56, 54, 52,
  324. 50, 48, 46, 44, 41, 39, 37, 35,
  325. 32, 30, 28, 26, 23, 21, 19, 16,
  326. 14, 12, 10, 7, 5, 3, 0, -1,
  327. -3, -6, -8, -10, -13, -15, -17, -19,
  328. -22, -24, -26, -29, -31, -33, -35, -38,
  329. -40, -42, -44, -46, -48, -51, -53, -55,
  330. -57, -59, -61, -63, -65, -67, -69, -71,
  331. -73, -75, -77, -79, -81, -82, -84, -86,
  332. -88, -89, -91, -93, -94, -96, -98, -99,
  333. -101, -102, -104, -105, -106, -108, -109, -110,
  334. -112, -113, -114, -115, -116, -117, -119, -120,
  335. -120, -121, -122, -123, -124, -125, -126, -126,
  336. -127, -128, -128, -128, -128, -128, -128, -128,
  337. -128, -128, -128, -128, -128, -128, -128, -128,
  338. -128, -128, -128, -128, -128, -128, -128, -128,
  339. -128, -128, -128, -128, -128, -128, -128, -128,
  340. -127, -127, -126, -125, -125, -124, -123, -122,
  341. -121, -120, -119, -118, -117, -116, -115, -114,
  342. -113, -111, -110, -109, -107, -106, -105, -103,
  343. -102, -100, -99, -97, -96, -94, -92, -91,
  344. -89, -87, -85, -84, -82, -80, -78, -76,
  345. -74, -73, -71, -69, -67, -65, -63, -61,
  346. -58, -56, -54, -52, -50, -48, -46, -44,
  347. -41, -39, -37, -35, -32, -30, -28, -26,
  348. -23, -21, -19, -16, -14, -12, -10, -7,
  349. -5, -3, 0, 1, 3, 6, 8, 10,
  350. 13, 15, 17, 19, 22, 24, 26, 29,
  351. 31, 33, 35, 38, 40, 42, 44, 46,
  352. 48, 51, 53, 55, 57, 59, 61, 63,
  353. 65, 67, 69, 71, 73, 75, 77, 79,
  354. 81, 82, 84, 86, 88, 89, 91, 93,
  355. 94, 96, 98, 99, 101, 102, 104, 105,
  356. 106, 108, 109, 110, 112, 113, 114, 115,
  357. 116, 117, 119, 120, 120, 121, 122, 123,
  358. 124, 125, 126, 126, 127, 128, 128, 129,
  359. 129, 130, 130, 131, 131, 131, 131, 132,
  360. 132, 132, 132, 132, 132, 132, 132, 132,
  361. 132, 132, 132, 131, 131, 131, 130, 130,
  362. 130, 129, 129, 128, 127, 127, 126, 125,
  363. 125, 124, 123, 122, 121, 120, 119, 118,
  364. 117, 116, 115, 114, 113, 111, 110, 109,
  365. 107, 106, 105, 103, 102, 100, 99, 97,
  366. 96, 94, 92, 91, 89, 87, 85, 84, 82
  367. };
  368. static const s16 hsv_green_x[] = {
  369. -124, -124, -125, -125, -125, -125, -125, -125,
  370. -125, -126, -126, -125, -125, -125, -125, -125,
  371. -125, -124, -124, -124, -123, -123, -122, -122,
  372. -121, -121, -120, -120, -119, -118, -117, -117,
  373. -116, -115, -114, -113, -112, -111, -110, -109,
  374. -108, -107, -105, -104, -103, -102, -100, -99,
  375. -98, -96, -95, -93, -92, -91, -89, -87,
  376. -86, -84, -83, -81, -79, -77, -76, -74,
  377. -72, -70, -69, -67, -65, -63, -61, -59,
  378. -57, -55, -53, -51, -49, -47, -45, -43,
  379. -41, -39, -37, -35, -33, -30, -28, -26,
  380. -24, -22, -20, -18, -15, -13, -11, -9,
  381. -7, -4, -2, 0, 1, 3, 6, 8,
  382. 10, 12, 14, 17, 19, 21, 23, 25,
  383. 27, 29, 32, 34, 36, 38, 40, 42,
  384. 44, 46, 48, 50, 52, 54, 56, 58,
  385. 60, 62, 64, 66, 68, 70, 71, 73,
  386. 75, 77, 78, 80, 82, 83, 85, 87,
  387. 88, 90, 91, 93, 94, 96, 97, 98,
  388. 100, 101, 102, 104, 105, 106, 107, 108,
  389. 109, 111, 112, 113, 113, 114, 115, 116,
  390. 117, 118, 118, 119, 120, 120, 121, 122,
  391. 122, 123, 123, 124, 124, 124, 125, 125,
  392. 125, 125, 125, 125, 125, 126, 126, 125,
  393. 125, 125, 125, 125, 125, 124, 124, 124,
  394. 123, 123, 122, 122, 121, 121, 120, 120,
  395. 119, 118, 117, 117, 116, 115, 114, 113,
  396. 112, 111, 110, 109, 108, 107, 105, 104,
  397. 103, 102, 100, 99, 98, 96, 95, 93,
  398. 92, 91, 89, 87, 86, 84, 83, 81,
  399. 79, 77, 76, 74, 72, 70, 69, 67,
  400. 65, 63, 61, 59, 57, 55, 53, 51,
  401. 49, 47, 45, 43, 41, 39, 37, 35,
  402. 33, 30, 28, 26, 24, 22, 20, 18,
  403. 15, 13, 11, 9, 7, 4, 2, 0,
  404. -1, -3, -6, -8, -10, -12, -14, -17,
  405. -19, -21, -23, -25, -27, -29, -32, -34,
  406. -36, -38, -40, -42, -44, -46, -48, -50,
  407. -52, -54, -56, -58, -60, -62, -64, -66,
  408. -68, -70, -71, -73, -75, -77, -78, -80,
  409. -82, -83, -85, -87, -88, -90, -91, -93,
  410. -94, -96, -97, -98, -100, -101, -102, -104,
  411. -105, -106, -107, -108, -109, -111, -112, -113,
  412. -113, -114, -115, -116, -117, -118, -118, -119,
  413. -120, -120, -121, -122, -122, -123, -123, -124, -124
  414. };
  415. static const s16 hsv_green_y[] = {
  416. -100, -99, -98, -97, -95, -94, -93, -91,
  417. -90, -89, -87, -86, -84, -83, -81, -80,
  418. -78, -76, -75, -73, -71, -70, -68, -66,
  419. -64, -63, -61, -59, -57, -55, -53, -51,
  420. -49, -48, -46, -44, -42, -40, -38, -36,
  421. -34, -32, -30, -27, -25, -23, -21, -19,
  422. -17, -15, -13, -11, -9, -7, -4, -2,
  423. 0, 1, 3, 5, 7, 9, 11, 14,
  424. 16, 18, 20, 22, 24, 26, 28, 30,
  425. 32, 34, 36, 38, 40, 42, 44, 46,
  426. 48, 50, 52, 54, 56, 58, 59, 61,
  427. 63, 65, 67, 68, 70, 72, 74, 75,
  428. 77, 78, 80, 82, 83, 85, 86, 88,
  429. 89, 90, 92, 93, 95, 96, 97, 98,
  430. 100, 101, 102, 103, 104, 105, 106, 107,
  431. 108, 109, 110, 111, 112, 112, 113, 114,
  432. 115, 115, 116, 116, 117, 117, 118, 118,
  433. 119, 119, 119, 120, 120, 120, 120, 120,
  434. 121, 121, 121, 121, 121, 121, 120, 120,
  435. 120, 120, 120, 119, 119, 119, 118, 118,
  436. 117, 117, 116, 116, 115, 114, 114, 113,
  437. 112, 111, 111, 110, 109, 108, 107, 106,
  438. 105, 104, 103, 102, 100, 99, 98, 97,
  439. 95, 94, 93, 91, 90, 89, 87, 86,
  440. 84, 83, 81, 80, 78, 76, 75, 73,
  441. 71, 70, 68, 66, 64, 63, 61, 59,
  442. 57, 55, 53, 51, 49, 48, 46, 44,
  443. 42, 40, 38, 36, 34, 32, 30, 27,
  444. 25, 23, 21, 19, 17, 15, 13, 11,
  445. 9, 7, 4, 2, 0, -1, -3, -5,
  446. -7, -9, -11, -14, -16, -18, -20, -22,
  447. -24, -26, -28, -30, -32, -34, -36, -38,
  448. -40, -42, -44, -46, -48, -50, -52, -54,
  449. -56, -58, -59, -61, -63, -65, -67, -68,
  450. -70, -72, -74, -75, -77, -78, -80, -82,
  451. -83, -85, -86, -88, -89, -90, -92, -93,
  452. -95, -96, -97, -98, -100, -101, -102, -103,
  453. -104, -105, -106, -107, -108, -109, -110, -111,
  454. -112, -112, -113, -114, -115, -115, -116, -116,
  455. -117, -117, -118, -118, -119, -119, -119, -120,
  456. -120, -120, -120, -120, -121, -121, -121, -121,
  457. -121, -121, -120, -120, -120, -120, -120, -119,
  458. -119, -119, -118, -118, -117, -117, -116, -116,
  459. -115, -114, -114, -113, -112, -111, -111, -110,
  460. -109, -108, -107, -106, -105, -104, -103, -102, -100
  461. };
  462. static const s16 hsv_blue_x[] = {
  463. 112, 113, 114, 114, 115, 116, 117, 117,
  464. 118, 118, 119, 119, 120, 120, 120, 121,
  465. 121, 121, 122, 122, 122, 122, 122, 122,
  466. 122, 122, 122, 122, 122, 122, 121, 121,
  467. 121, 120, 120, 120, 119, 119, 118, 118,
  468. 117, 116, 116, 115, 114, 113, 113, 112,
  469. 111, 110, 109, 108, 107, 106, 105, 104,
  470. 103, 102, 100, 99, 98, 97, 95, 94,
  471. 93, 91, 90, 88, 87, 85, 84, 82,
  472. 80, 79, 77, 76, 74, 72, 70, 69,
  473. 67, 65, 63, 61, 60, 58, 56, 54,
  474. 52, 50, 48, 46, 44, 42, 40, 38,
  475. 36, 34, 32, 30, 28, 26, 24, 22,
  476. 19, 17, 15, 13, 11, 9, 7, 5,
  477. 2, 0, -1, -3, -5, -7, -9, -12,
  478. -14, -16, -18, -20, -22, -24, -26, -28,
  479. -31, -33, -35, -37, -39, -41, -43, -45,
  480. -47, -49, -51, -53, -54, -56, -58, -60,
  481. -62, -64, -66, -67, -69, -71, -73, -74,
  482. -76, -78, -79, -81, -83, -84, -86, -87,
  483. -89, -90, -92, -93, -94, -96, -97, -98,
  484. -99, -101, -102, -103, -104, -105, -106, -107,
  485. -108, -109, -110, -111, -112, -113, -114, -114,
  486. -115, -116, -117, -117, -118, -118, -119, -119,
  487. -120, -120, -120, -121, -121, -121, -122, -122,
  488. -122, -122, -122, -122, -122, -122, -122, -122,
  489. -122, -122, -121, -121, -121, -120, -120, -120,
  490. -119, -119, -118, -118, -117, -116, -116, -115,
  491. -114, -113, -113, -112, -111, -110, -109, -108,
  492. -107, -106, -105, -104, -103, -102, -100, -99,
  493. -98, -97, -95, -94, -93, -91, -90, -88,
  494. -87, -85, -84, -82, -80, -79, -77, -76,
  495. -74, -72, -70, -69, -67, -65, -63, -61,
  496. -60, -58, -56, -54, -52, -50, -48, -46,
  497. -44, -42, -40, -38, -36, -34, -32, -30,
  498. -28, -26, -24, -22, -19, -17, -15, -13,
  499. -11, -9, -7, -5, -2, 0, 1, 3,
  500. 5, 7, 9, 12, 14, 16, 18, 20,
  501. 22, 24, 26, 28, 31, 33, 35, 37,
  502. 39, 41, 43, 45, 47, 49, 51, 53,
  503. 54, 56, 58, 60, 62, 64, 66, 67,
  504. 69, 71, 73, 74, 76, 78, 79, 81,
  505. 83, 84, 86, 87, 89, 90, 92, 93,
  506. 94, 96, 97, 98, 99, 101, 102, 103,
  507. 104, 105, 106, 107, 108, 109, 110, 111, 112
  508. };
  509. static const s16 hsv_blue_y[] = {
  510. -11, -13, -15, -17, -19, -21, -23, -25,
  511. -27, -29, -31, -33, -35, -37, -39, -41,
  512. -43, -45, -46, -48, -50, -52, -54, -55,
  513. -57, -59, -61, -62, -64, -66, -67, -69,
  514. -71, -72, -74, -75, -77, -78, -80, -81,
  515. -83, -84, -86, -87, -88, -90, -91, -92,
  516. -93, -95, -96, -97, -98, -99, -100, -101,
  517. -102, -103, -104, -105, -106, -106, -107, -108,
  518. -109, -109, -110, -111, -111, -112, -112, -113,
  519. -113, -114, -114, -114, -115, -115, -115, -115,
  520. -116, -116, -116, -116, -116, -116, -116, -116,
  521. -116, -115, -115, -115, -115, -114, -114, -114,
  522. -113, -113, -112, -112, -111, -111, -110, -110,
  523. -109, -108, -108, -107, -106, -105, -104, -103,
  524. -102, -101, -100, -99, -98, -97, -96, -95,
  525. -94, -93, -91, -90, -89, -88, -86, -85,
  526. -84, -82, -81, -79, -78, -76, -75, -73,
  527. -71, -70, -68, -67, -65, -63, -62, -60,
  528. -58, -56, -55, -53, -51, -49, -47, -45,
  529. -44, -42, -40, -38, -36, -34, -32, -30,
  530. -28, -26, -24, -22, -20, -18, -16, -14,
  531. -12, -10, -8, -6, -4, -2, 0, 1,
  532. 3, 5, 7, 9, 11, 13, 15, 17,
  533. 19, 21, 23, 25, 27, 29, 31, 33,
  534. 35, 37, 39, 41, 43, 45, 46, 48,
  535. 50, 52, 54, 55, 57, 59, 61, 62,
  536. 64, 66, 67, 69, 71, 72, 74, 75,
  537. 77, 78, 80, 81, 83, 84, 86, 87,
  538. 88, 90, 91, 92, 93, 95, 96, 97,
  539. 98, 99, 100, 101, 102, 103, 104, 105,
  540. 106, 106, 107, 108, 109, 109, 110, 111,
  541. 111, 112, 112, 113, 113, 114, 114, 114,
  542. 115, 115, 115, 115, 116, 116, 116, 116,
  543. 116, 116, 116, 116, 116, 115, 115, 115,
  544. 115, 114, 114, 114, 113, 113, 112, 112,
  545. 111, 111, 110, 110, 109, 108, 108, 107,
  546. 106, 105, 104, 103, 102, 101, 100, 99,
  547. 98, 97, 96, 95, 94, 93, 91, 90,
  548. 89, 88, 86, 85, 84, 82, 81, 79,
  549. 78, 76, 75, 73, 71, 70, 68, 67,
  550. 65, 63, 62, 60, 58, 56, 55, 53,
  551. 51, 49, 47, 45, 44, 42, 40, 38,
  552. 36, 34, 32, 30, 28, 26, 24, 22,
  553. 20, 18, 16, 14, 12, 10, 8, 6,
  554. 4, 2, 0, -1, -3, -5, -7, -9, -11
  555. };
  556. static const u16 i2c_ident[] = {
  557. V4L2_IDENT_OV9650,
  558. V4L2_IDENT_OV9655,
  559. V4L2_IDENT_SOI968,
  560. V4L2_IDENT_OV7660,
  561. V4L2_IDENT_OV7670,
  562. V4L2_IDENT_MT9V011,
  563. V4L2_IDENT_MT9V111,
  564. V4L2_IDENT_MT9V112,
  565. V4L2_IDENT_MT9M001C12ST,
  566. V4L2_IDENT_MT9M111,
  567. V4L2_IDENT_MT9M112,
  568. V4L2_IDENT_HV7131R,
  569. [SENSOR_MT9VPRB] = V4L2_IDENT_UNKNOWN,
  570. };
  571. static const u16 bridge_init[][2] = {
  572. {0x1000, 0x78}, {0x1001, 0x40}, {0x1002, 0x1c},
  573. {0x1020, 0x80}, {0x1061, 0x01}, {0x1067, 0x40},
  574. {0x1068, 0x30}, {0x1069, 0x20}, {0x106a, 0x10},
  575. {0x106b, 0x08}, {0x1188, 0x87}, {0x11a1, 0x00},
  576. {0x11a2, 0x00}, {0x11a3, 0x6a}, {0x11a4, 0x50},
  577. {0x11ab, 0x00}, {0x11ac, 0x00}, {0x11ad, 0x50},
  578. {0x11ae, 0x3c}, {0x118a, 0x04}, {0x0395, 0x04},
  579. {0x11b8, 0x3a}, {0x118b, 0x0e}, {0x10f7, 0x05},
  580. {0x10f8, 0x14}, {0x10fa, 0xff}, {0x10f9, 0x00},
  581. {0x11ba, 0x0a}, {0x11a5, 0x2d}, {0x11a6, 0x2d},
  582. {0x11a7, 0x3a}, {0x11a8, 0x05}, {0x11a9, 0x04},
  583. {0x11aa, 0x3f}, {0x11af, 0x28}, {0x11b0, 0xd8},
  584. {0x11b1, 0x14}, {0x11b2, 0xec}, {0x11b3, 0x32},
  585. {0x11b4, 0xdd}, {0x11b5, 0x32}, {0x11b6, 0xdd},
  586. {0x10e0, 0x2c}, {0x11bc, 0x40}, {0x11bd, 0x01},
  587. {0x11be, 0xf0}, {0x11bf, 0x00}, {0x118c, 0x1f},
  588. {0x118d, 0x1f}, {0x118e, 0x1f}, {0x118f, 0x1f},
  589. {0x1180, 0x01}, {0x1181, 0x00}, {0x1182, 0x01},
  590. {0x1183, 0x00}, {0x1184, 0x50}, {0x1185, 0x80},
  591. {0x1007, 0x00}
  592. };
  593. /* Gain = (bit[3:0] / 16 + 1) * (bit[4] + 1) * (bit[5] + 1) * (bit[6] + 1) */
  594. static const u8 ov_gain[] = {
  595. 0x00 /* 1x */, 0x04 /* 1.25x */, 0x08 /* 1.5x */, 0x0c /* 1.75x */,
  596. 0x10 /* 2x */, 0x12 /* 2.25x */, 0x14 /* 2.5x */, 0x16 /* 2.75x */,
  597. 0x18 /* 3x */, 0x1a /* 3.25x */, 0x1c /* 3.5x */, 0x1e /* 3.75x */,
  598. 0x30 /* 4x */, 0x31 /* 4.25x */, 0x32 /* 4.5x */, 0x33 /* 4.75x */,
  599. 0x34 /* 5x */, 0x35 /* 5.25x */, 0x36 /* 5.5x */, 0x37 /* 5.75x */,
  600. 0x38 /* 6x */, 0x39 /* 6.25x */, 0x3a /* 6.5x */, 0x3b /* 6.75x */,
  601. 0x3c /* 7x */, 0x3d /* 7.25x */, 0x3e /* 7.5x */, 0x3f /* 7.75x */,
  602. 0x70 /* 8x */
  603. };
  604. /* Gain = (bit[8] + 1) * (bit[7] + 1) * (bit[6:0] * 0.03125) */
  605. static const u16 micron1_gain[] = {
  606. /* 1x 1.25x 1.5x 1.75x */
  607. 0x0020, 0x0028, 0x0030, 0x0038,
  608. /* 2x 2.25x 2.5x 2.75x */
  609. 0x00a0, 0x00a4, 0x00a8, 0x00ac,
  610. /* 3x 3.25x 3.5x 3.75x */
  611. 0x00b0, 0x00b4, 0x00b8, 0x00bc,
  612. /* 4x 4.25x 4.5x 4.75x */
  613. 0x00c0, 0x00c4, 0x00c8, 0x00cc,
  614. /* 5x 5.25x 5.5x 5.75x */
  615. 0x00d0, 0x00d4, 0x00d8, 0x00dc,
  616. /* 6x 6.25x 6.5x 6.75x */
  617. 0x00e0, 0x00e4, 0x00e8, 0x00ec,
  618. /* 7x 7.25x 7.5x 7.75x */
  619. 0x00f0, 0x00f4, 0x00f8, 0x00fc,
  620. /* 8x */
  621. 0x01c0
  622. };
  623. /* mt9m001 sensor uses a different gain formula then other micron sensors */
  624. /* Gain = (bit[6] + 1) * (bit[5-0] * 0.125) */
  625. static const u16 micron2_gain[] = {
  626. /* 1x 1.25x 1.5x 1.75x */
  627. 0x0008, 0x000a, 0x000c, 0x000e,
  628. /* 2x 2.25x 2.5x 2.75x */
  629. 0x0010, 0x0012, 0x0014, 0x0016,
  630. /* 3x 3.25x 3.5x 3.75x */
  631. 0x0018, 0x001a, 0x001c, 0x001e,
  632. /* 4x 4.25x 4.5x 4.75x */
  633. 0x0020, 0x0051, 0x0052, 0x0053,
  634. /* 5x 5.25x 5.5x 5.75x */
  635. 0x0054, 0x0055, 0x0056, 0x0057,
  636. /* 6x 6.25x 6.5x 6.75x */
  637. 0x0058, 0x0059, 0x005a, 0x005b,
  638. /* 7x 7.25x 7.5x 7.75x */
  639. 0x005c, 0x005d, 0x005e, 0x005f,
  640. /* 8x */
  641. 0x0060
  642. };
  643. /* Gain = .5 + bit[7:0] / 16 */
  644. static const u8 hv7131r_gain[] = {
  645. 0x08 /* 1x */, 0x0c /* 1.25x */, 0x10 /* 1.5x */, 0x14 /* 1.75x */,
  646. 0x18 /* 2x */, 0x1c /* 2.25x */, 0x20 /* 2.5x */, 0x24 /* 2.75x */,
  647. 0x28 /* 3x */, 0x2c /* 3.25x */, 0x30 /* 3.5x */, 0x34 /* 3.75x */,
  648. 0x38 /* 4x */, 0x3c /* 4.25x */, 0x40 /* 4.5x */, 0x44 /* 4.75x */,
  649. 0x48 /* 5x */, 0x4c /* 5.25x */, 0x50 /* 5.5x */, 0x54 /* 5.75x */,
  650. 0x58 /* 6x */, 0x5c /* 6.25x */, 0x60 /* 6.5x */, 0x64 /* 6.75x */,
  651. 0x68 /* 7x */, 0x6c /* 7.25x */, 0x70 /* 7.5x */, 0x74 /* 7.75x */,
  652. 0x78 /* 8x */
  653. };
  654. static const struct i2c_reg_u8 soi968_init[] = {
  655. {0x0c, 0x00}, {0x0f, 0x1f},
  656. {0x11, 0x80}, {0x38, 0x52}, {0x1e, 0x00},
  657. {0x33, 0x08}, {0x35, 0x8c}, {0x36, 0x0c},
  658. {0x37, 0x04}, {0x45, 0x04}, {0x47, 0xff},
  659. {0x3e, 0x00}, {0x3f, 0x00}, {0x3b, 0x20},
  660. {0x3a, 0x96}, {0x3d, 0x0a}, {0x14, 0x8e},
  661. {0x13, 0x8b}, {0x12, 0x40}, {0x17, 0x13},
  662. {0x18, 0x63}, {0x19, 0x01}, {0x1a, 0x79},
  663. {0x32, 0x24}, {0x03, 0x00}, {0x11, 0x40},
  664. {0x2a, 0x10}, {0x2b, 0xe0}, {0x10, 0x32},
  665. {0x00, 0x00}, {0x01, 0x80}, {0x02, 0x80},
  666. };
  667. static const struct i2c_reg_u8 ov7660_init[] = {
  668. {0x0e, 0x80}, {0x0d, 0x08}, {0x0f, 0xc3},
  669. {0x04, 0xc3}, {0x10, 0x40}, {0x11, 0x40},
  670. {0x12, 0x05}, {0x13, 0xba}, {0x14, 0x2a},
  671. /* HDG Set hstart and hstop, datasheet default 0x11, 0x61, using
  672. 0x10, 0x61 and sd->hstart, vstart = 3, fixes ugly colored borders */
  673. {0x17, 0x10}, {0x18, 0x61},
  674. {0x37, 0x0f}, {0x38, 0x02}, {0x39, 0x43},
  675. {0x3a, 0x00}, {0x69, 0x90}, {0x2d, 0x00},
  676. {0x2e, 0x00}, {0x01, 0x78}, {0x02, 0x50},
  677. };
  678. static const struct i2c_reg_u8 ov7670_init[] = {
  679. {0x11, 0x80}, {0x3a, 0x04}, {0x12, 0x01},
  680. {0x32, 0xb6}, {0x03, 0x0a}, {0x0c, 0x00}, {0x3e, 0x00},
  681. {0x70, 0x3a}, {0x71, 0x35}, {0x72, 0x11}, {0x73, 0xf0},
  682. {0xa2, 0x02}, {0x13, 0xe0}, {0x00, 0x00}, {0x10, 0x00},
  683. {0x0d, 0x40}, {0x14, 0x28}, {0xa5, 0x05}, {0xab, 0x07},
  684. {0x24, 0x95}, {0x25, 0x33}, {0x26, 0xe3}, {0x9f, 0x75},
  685. {0xa0, 0x65}, {0xa1, 0x0b}, {0xa6, 0xd8}, {0xa7, 0xd8},
  686. {0xa8, 0xf0}, {0xa9, 0x90}, {0xaa, 0x94}, {0x13, 0xe5},
  687. {0x0e, 0x61}, {0x0f, 0x4b}, {0x16, 0x02}, {0x1e, 0x27},
  688. {0x21, 0x02}, {0x22, 0x91}, {0x29, 0x07}, {0x33, 0x0b},
  689. {0x35, 0x0b}, {0x37, 0x1d}, {0x38, 0x71}, {0x39, 0x2a},
  690. {0x3c, 0x78}, {0x4d, 0x40}, {0x4e, 0x20}, {0x69, 0x00},
  691. {0x74, 0x19}, {0x8d, 0x4f}, {0x8e, 0x00}, {0x8f, 0x00},
  692. {0x90, 0x00}, {0x91, 0x00}, {0x96, 0x00}, {0x9a, 0x80},
  693. {0xb0, 0x84}, {0xb1, 0x0c}, {0xb2, 0x0e}, {0xb3, 0x82},
  694. {0xb8, 0x0a}, {0x43, 0x0a}, {0x44, 0xf0}, {0x45, 0x20},
  695. {0x46, 0x7d}, {0x47, 0x29}, {0x48, 0x4a}, {0x59, 0x8c},
  696. {0x5a, 0xa5}, {0x5b, 0xde}, {0x5c, 0x96}, {0x5d, 0x66},
  697. {0x5e, 0x10}, {0x6c, 0x0a}, {0x6d, 0x55}, {0x6e, 0x11},
  698. {0x6f, 0x9e}, {0x6a, 0x40}, {0x01, 0x40}, {0x02, 0x40},
  699. {0x13, 0xe7}, {0x4f, 0x6e}, {0x50, 0x70}, {0x51, 0x02},
  700. {0x52, 0x1d}, {0x53, 0x56}, {0x54, 0x73}, {0x55, 0x0a},
  701. {0x56, 0x55}, {0x57, 0x80}, {0x58, 0x9e}, {0x41, 0x08},
  702. {0x3f, 0x02}, {0x75, 0x03}, {0x76, 0x63}, {0x4c, 0x04},
  703. {0x77, 0x06}, {0x3d, 0x02}, {0x4b, 0x09}, {0xc9, 0x30},
  704. {0x41, 0x08}, {0x56, 0x48}, {0x34, 0x11}, {0xa4, 0x88},
  705. {0x96, 0x00}, {0x97, 0x30}, {0x98, 0x20}, {0x99, 0x30},
  706. {0x9a, 0x84}, {0x9b, 0x29}, {0x9c, 0x03}, {0x9d, 0x99},
  707. {0x9e, 0x7f}, {0x78, 0x04}, {0x79, 0x01}, {0xc8, 0xf0},
  708. {0x79, 0x0f}, {0xc8, 0x00}, {0x79, 0x10}, {0xc8, 0x7e},
  709. {0x79, 0x0a}, {0xc8, 0x80}, {0x79, 0x0b}, {0xc8, 0x01},
  710. {0x79, 0x0c}, {0xc8, 0x0f}, {0x79, 0x0d}, {0xc8, 0x20},
  711. {0x79, 0x09}, {0xc8, 0x80}, {0x79, 0x02}, {0xc8, 0xc0},
  712. {0x79, 0x03}, {0xc8, 0x40}, {0x79, 0x05}, {0xc8, 0x30},
  713. {0x79, 0x26}, {0x62, 0x20}, {0x63, 0x00}, {0x64, 0x06},
  714. {0x65, 0x00}, {0x66, 0x05}, {0x94, 0x05}, {0x95, 0x0a},
  715. {0x17, 0x13}, {0x18, 0x01}, {0x19, 0x02}, {0x1a, 0x7a},
  716. {0x46, 0x59}, {0x47, 0x30}, {0x58, 0x9a}, {0x59, 0x84},
  717. {0x5a, 0x91}, {0x5b, 0x57}, {0x5c, 0x75}, {0x5d, 0x6d},
  718. {0x5e, 0x13}, {0x64, 0x07}, {0x94, 0x07}, {0x95, 0x0d},
  719. {0xa6, 0xdf}, {0xa7, 0xdf}, {0x48, 0x4d}, {0x51, 0x00},
  720. {0x6b, 0x0a}, {0x11, 0x80}, {0x2a, 0x00}, {0x2b, 0x00},
  721. {0x92, 0x00}, {0x93, 0x00}, {0x55, 0x0a}, {0x56, 0x60},
  722. {0x4f, 0x6e}, {0x50, 0x70}, {0x51, 0x00}, {0x52, 0x1d},
  723. {0x53, 0x56}, {0x54, 0x73}, {0x58, 0x9a}, {0x4f, 0x6e},
  724. {0x50, 0x70}, {0x51, 0x00}, {0x52, 0x1d}, {0x53, 0x56},
  725. {0x54, 0x73}, {0x58, 0x9a}, {0x3f, 0x01}, {0x7b, 0x03},
  726. {0x7c, 0x09}, {0x7d, 0x16}, {0x7e, 0x38}, {0x7f, 0x47},
  727. {0x80, 0x53}, {0x81, 0x5e}, {0x82, 0x6a}, {0x83, 0x74},
  728. {0x84, 0x80}, {0x85, 0x8c}, {0x86, 0x9b}, {0x87, 0xb2},
  729. {0x88, 0xcc}, {0x89, 0xe5}, {0x7a, 0x24}, {0x3b, 0x00},
  730. {0x9f, 0x76}, {0xa0, 0x65}, {0x13, 0xe2}, {0x6b, 0x0a},
  731. {0x11, 0x80}, {0x2a, 0x00}, {0x2b, 0x00}, {0x92, 0x00},
  732. {0x93, 0x00},
  733. };
  734. static const struct i2c_reg_u8 ov9650_init[] = {
  735. {0x00, 0x00}, {0x01, 0x78},
  736. {0x02, 0x78}, {0x03, 0x36}, {0x04, 0x03},
  737. {0x05, 0x00}, {0x06, 0x00}, {0x08, 0x00},
  738. {0x09, 0x01}, {0x0c, 0x00}, {0x0d, 0x00},
  739. {0x0e, 0xa0}, {0x0f, 0x52}, {0x10, 0x7c},
  740. {0x11, 0x80}, {0x12, 0x45}, {0x13, 0xc2},
  741. {0x14, 0x2e}, {0x15, 0x00}, {0x16, 0x07},
  742. {0x17, 0x24}, {0x18, 0xc5}, {0x19, 0x00},
  743. {0x1a, 0x3c}, {0x1b, 0x00}, {0x1e, 0x04},
  744. {0x1f, 0x00}, {0x24, 0x78}, {0x25, 0x68},
  745. {0x26, 0xd4}, {0x27, 0x80}, {0x28, 0x80},
  746. {0x29, 0x30}, {0x2a, 0x00}, {0x2b, 0x00},
  747. {0x2c, 0x80}, {0x2d, 0x00}, {0x2e, 0x00},
  748. {0x2f, 0x00}, {0x30, 0x08}, {0x31, 0x30},
  749. {0x32, 0x84}, {0x33, 0xe2}, {0x34, 0xbf},
  750. {0x35, 0x81}, {0x36, 0xf9}, {0x37, 0x00},
  751. {0x38, 0x93}, {0x39, 0x50}, {0x3a, 0x01},
  752. {0x3b, 0x01}, {0x3c, 0x73}, {0x3d, 0x19},
  753. {0x3e, 0x0b}, {0x3f, 0x80}, {0x40, 0xc1},
  754. {0x41, 0x00}, {0x42, 0x08}, {0x67, 0x80},
  755. {0x68, 0x80}, {0x69, 0x40}, {0x6a, 0x00},
  756. {0x6b, 0x0a}, {0x8b, 0x06}, {0x8c, 0x20},
  757. {0x8d, 0x00}, {0x8e, 0x00}, {0x8f, 0xdf},
  758. {0x92, 0x00}, {0x93, 0x00}, {0x94, 0x88},
  759. {0x95, 0x88}, {0x96, 0x04}, {0xa1, 0x00},
  760. {0xa5, 0x80}, {0xa8, 0x80}, {0xa9, 0xb8},
  761. {0xaa, 0x92}, {0xab, 0x0a},
  762. };
  763. static const struct i2c_reg_u8 ov9655_init[] = {
  764. {0x0e, 0x61}, {0x11, 0x80}, {0x13, 0xba},
  765. {0x14, 0x2e}, {0x16, 0x24}, {0x1e, 0x04}, {0x27, 0x08},
  766. {0x28, 0x08}, {0x29, 0x15}, {0x2c, 0x08}, {0x34, 0x3d},
  767. {0x35, 0x00}, {0x38, 0x12}, {0x0f, 0x42}, {0x39, 0x57},
  768. {0x3a, 0x00}, {0x3b, 0xcc}, {0x3c, 0x0c}, {0x3d, 0x19},
  769. {0x3e, 0x0c}, {0x3f, 0x01}, {0x41, 0x40}, {0x42, 0x80},
  770. {0x45, 0x46}, {0x46, 0x62}, {0x47, 0x2a}, {0x48, 0x3c},
  771. {0x4a, 0xf0}, {0x4b, 0xdc}, {0x4c, 0xdc}, {0x4d, 0xdc},
  772. {0x4e, 0xdc}, {0x6c, 0x04}, {0x6f, 0x9e}, {0x70, 0x05},
  773. {0x71, 0x78}, {0x77, 0x02}, {0x8a, 0x23}, {0x90, 0x7e},
  774. {0x91, 0x7c}, {0x9f, 0x6e}, {0xa0, 0x6e}, {0xa5, 0x68},
  775. {0xa6, 0x60}, {0xa8, 0xc1}, {0xa9, 0xfa}, {0xaa, 0x92},
  776. {0xab, 0x04}, {0xac, 0x80}, {0xad, 0x80}, {0xae, 0x80},
  777. {0xaf, 0x80}, {0xb2, 0xf2}, {0xb3, 0x20}, {0xb5, 0x00},
  778. {0xb6, 0xaf}, {0xbb, 0xae}, {0xbc, 0x44}, {0xbd, 0x44},
  779. {0xbe, 0x3b}, {0xbf, 0x3a}, {0xc1, 0xc8}, {0xc2, 0x01},
  780. {0xc4, 0x00}, {0xc6, 0x85}, {0xc7, 0x81}, {0xc9, 0xe0},
  781. {0xca, 0xe8}, {0xcc, 0xd8}, {0xcd, 0x93}, {0x2d, 0x00},
  782. {0x2e, 0x00}, {0x01, 0x80}, {0x02, 0x80}, {0x12, 0x61},
  783. {0x36, 0xfa}, {0x8c, 0x8d}, {0xc0, 0xaa}, {0x69, 0x0a},
  784. {0x03, 0x09}, {0x17, 0x16}, {0x18, 0x6e}, {0x19, 0x01},
  785. {0x1a, 0x3e}, {0x32, 0x09}, {0x2a, 0x10}, {0x2b, 0x0a},
  786. {0x92, 0x00}, {0x93, 0x00}, {0xa1, 0x00}, {0x10, 0x7c},
  787. {0x04, 0x03}, {0x00, 0x13},
  788. };
  789. static const struct i2c_reg_u16 mt9v112_init[] = {
  790. {0xf0, 0x0000}, {0x0d, 0x0021}, {0x0d, 0x0020},
  791. {0x34, 0xc019}, {0x0a, 0x0011}, {0x0b, 0x000b},
  792. {0x20, 0x0703}, {0x35, 0x2022}, {0xf0, 0x0001},
  793. {0x05, 0x0000}, {0x06, 0x340c}, {0x3b, 0x042a},
  794. {0x3c, 0x0400}, {0xf0, 0x0002}, {0x2e, 0x0c58},
  795. {0x5b, 0x0001}, {0xc8, 0x9f0b}, {0xf0, 0x0001},
  796. {0x9b, 0x5300}, {0xf0, 0x0000}, {0x2b, 0x0020},
  797. {0x2c, 0x002a}, {0x2d, 0x0032}, {0x2e, 0x0020},
  798. {0x09, 0x01dc}, {0x01, 0x000c}, {0x02, 0x0020},
  799. {0x03, 0x01e0}, {0x04, 0x0280}, {0x06, 0x000c},
  800. {0x05, 0x0098}, {0x20, 0x0703}, {0x09, 0x01f2},
  801. {0x2b, 0x00a0}, {0x2c, 0x00a0}, {0x2d, 0x00a0},
  802. {0x2e, 0x00a0}, {0x01, 0x000c}, {0x02, 0x0020},
  803. {0x03, 0x01e0}, {0x04, 0x0280}, {0x06, 0x000c},
  804. {0x05, 0x0098}, {0x09, 0x01c1}, {0x2b, 0x00ae},
  805. {0x2c, 0x00ae}, {0x2d, 0x00ae}, {0x2e, 0x00ae},
  806. };
  807. static const struct i2c_reg_u16 mt9v111_init[] = {
  808. {0x01, 0x0004}, {0x0d, 0x0001}, {0x0d, 0x0000},
  809. {0x01, 0x0001}, {0x05, 0x0004}, {0x2d, 0xe0a0},
  810. {0x2e, 0x0c64}, {0x2f, 0x0064}, {0x06, 0x600e},
  811. {0x08, 0x0480}, {0x01, 0x0004}, {0x02, 0x0016},
  812. {0x03, 0x01e7}, {0x04, 0x0287}, {0x05, 0x0004},
  813. {0x06, 0x002d}, {0x07, 0x3002}, {0x08, 0x0008},
  814. {0x0e, 0x0008}, {0x20, 0x0000}
  815. };
  816. static const struct i2c_reg_u16 mt9v011_init[] = {
  817. {0x07, 0x0002}, {0x0d, 0x0001}, {0x0d, 0x0000},
  818. {0x01, 0x0008}, {0x02, 0x0016}, {0x03, 0x01e1},
  819. {0x04, 0x0281}, {0x05, 0x0083}, {0x06, 0x0006},
  820. {0x0d, 0x0002}, {0x0a, 0x0000}, {0x0b, 0x0000},
  821. {0x0c, 0x0000}, {0x0d, 0x0000}, {0x0e, 0x0000},
  822. {0x0f, 0x0000}, {0x10, 0x0000}, {0x11, 0x0000},
  823. {0x12, 0x0000}, {0x13, 0x0000}, {0x14, 0x0000},
  824. {0x15, 0x0000}, {0x16, 0x0000}, {0x17, 0x0000},
  825. {0x18, 0x0000}, {0x19, 0x0000}, {0x1a, 0x0000},
  826. {0x1b, 0x0000}, {0x1c, 0x0000}, {0x1d, 0x0000},
  827. {0x32, 0x0000}, {0x20, 0x1101}, {0x21, 0x0000},
  828. {0x22, 0x0000}, {0x23, 0x0000}, {0x24, 0x0000},
  829. {0x25, 0x0000}, {0x26, 0x0000}, {0x27, 0x0024},
  830. {0x2f, 0xf7b0}, {0x30, 0x0005}, {0x31, 0x0000},
  831. {0x32, 0x0000}, {0x33, 0x0000}, {0x34, 0x0100},
  832. {0x3d, 0x068f}, {0x40, 0x01e0}, {0x41, 0x00d1},
  833. {0x44, 0x0082}, {0x5a, 0x0000}, {0x5b, 0x0000},
  834. {0x5c, 0x0000}, {0x5d, 0x0000}, {0x5e, 0x0000},
  835. {0x5f, 0xa31d}, {0x62, 0x0611}, {0x0a, 0x0000},
  836. {0x06, 0x0029}, {0x05, 0x0009}, {0x20, 0x1101},
  837. {0x20, 0x1101}, {0x09, 0x0064}, {0x07, 0x0003},
  838. {0x2b, 0x0033}, {0x2c, 0x00a0}, {0x2d, 0x00a0},
  839. {0x2e, 0x0033}, {0x07, 0x0002}, {0x06, 0x0000},
  840. {0x06, 0x0029}, {0x05, 0x0009},
  841. };
  842. static const struct i2c_reg_u16 mt9m001_init[] = {
  843. {0x0d, 0x0001},
  844. {0x0d, 0x0000},
  845. {0x04, 0x0500}, /* hres = 1280 */
  846. {0x03, 0x0400}, /* vres = 1024 */
  847. {0x20, 0x1100},
  848. {0x06, 0x0010},
  849. {0x2b, 0x0024},
  850. {0x2e, 0x0024},
  851. {0x35, 0x0024},
  852. {0x2d, 0x0020},
  853. {0x2c, 0x0020},
  854. {0x09, 0x0ad4},
  855. {0x35, 0x0057},
  856. };
  857. static const struct i2c_reg_u16 mt9m111_init[] = {
  858. {0xf0, 0x0000}, {0x0d, 0x0021}, {0x0d, 0x0008},
  859. {0xf0, 0x0001}, {0x3a, 0x4300}, {0x9b, 0x4300},
  860. {0x06, 0x708e}, {0xf0, 0x0002}, {0x2e, 0x0a1e},
  861. {0xf0, 0x0000},
  862. };
  863. static const struct i2c_reg_u16 mt9m112_init[] = {
  864. {0xf0, 0x0000}, {0x0d, 0x0021}, {0x0d, 0x0008},
  865. {0xf0, 0x0001}, {0x3a, 0x4300}, {0x9b, 0x4300},
  866. {0x06, 0x708e}, {0xf0, 0x0002}, {0x2e, 0x0a1e},
  867. {0xf0, 0x0000},
  868. };
  869. static const struct i2c_reg_u8 hv7131r_init[] = {
  870. {0x02, 0x08}, {0x02, 0x00}, {0x01, 0x08},
  871. {0x02, 0x00}, {0x20, 0x00}, {0x21, 0xd0},
  872. {0x22, 0x00}, {0x23, 0x09}, {0x01, 0x08},
  873. {0x01, 0x08}, {0x01, 0x08}, {0x25, 0x07},
  874. {0x26, 0xc3}, {0x27, 0x50}, {0x30, 0x62},
  875. {0x31, 0x10}, {0x32, 0x06}, {0x33, 0x10},
  876. {0x20, 0x00}, {0x21, 0xd0}, {0x22, 0x00},
  877. {0x23, 0x09}, {0x01, 0x08},
  878. };
  879. static void reg_r(struct gspca_dev *gspca_dev, u16 reg, u16 length)
  880. {
  881. struct usb_device *dev = gspca_dev->dev;
  882. int result;
  883. if (gspca_dev->usb_err < 0)
  884. return;
  885. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  886. 0x00,
  887. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  888. reg,
  889. 0x00,
  890. gspca_dev->usb_buf,
  891. length,
  892. 500);
  893. if (unlikely(result < 0 || result != length)) {
  894. pr_err("Read register %02x failed %d\n", reg, result);
  895. gspca_dev->usb_err = result;
  896. }
  897. }
  898. static void reg_w(struct gspca_dev *gspca_dev, u16 reg,
  899. const u8 *buffer, int length)
  900. {
  901. struct usb_device *dev = gspca_dev->dev;
  902. int result;
  903. if (gspca_dev->usb_err < 0)
  904. return;
  905. memcpy(gspca_dev->usb_buf, buffer, length);
  906. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  907. 0x08,
  908. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  909. reg,
  910. 0x00,
  911. gspca_dev->usb_buf,
  912. length,
  913. 500);
  914. if (unlikely(result < 0 || result != length)) {
  915. pr_err("Write register %02x failed %d\n", reg, result);
  916. gspca_dev->usb_err = result;
  917. }
  918. }
  919. static void reg_w1(struct gspca_dev *gspca_dev, u16 reg, const u8 value)
  920. {
  921. reg_w(gspca_dev, reg, &value, 1);
  922. }
  923. static void i2c_w(struct gspca_dev *gspca_dev, const u8 *buffer)
  924. {
  925. int i;
  926. reg_w(gspca_dev, 0x10c0, buffer, 8);
  927. for (i = 0; i < 5; i++) {
  928. reg_r(gspca_dev, 0x10c0, 1);
  929. if (gspca_dev->usb_err < 0)
  930. return;
  931. if (gspca_dev->usb_buf[0] & 0x04) {
  932. if (gspca_dev->usb_buf[0] & 0x08) {
  933. pr_err("i2c_w error\n");
  934. gspca_dev->usb_err = -EIO;
  935. }
  936. return;
  937. }
  938. msleep(10);
  939. }
  940. pr_err("i2c_w reg %02x no response\n", buffer[2]);
  941. /* gspca_dev->usb_err = -EIO; fixme: may occur */
  942. }
  943. static void i2c_w1(struct gspca_dev *gspca_dev, u8 reg, u8 val)
  944. {
  945. struct sd *sd = (struct sd *) gspca_dev;
  946. u8 row[8];
  947. /*
  948. * from the point of view of the bridge, the length
  949. * includes the address
  950. */
  951. row[0] = sd->i2c_intf | (2 << 4);
  952. row[1] = sd->i2c_addr;
  953. row[2] = reg;
  954. row[3] = val;
  955. row[4] = 0x00;
  956. row[5] = 0x00;
  957. row[6] = 0x00;
  958. row[7] = 0x10;
  959. i2c_w(gspca_dev, row);
  960. }
  961. static void i2c_w1_buf(struct gspca_dev *gspca_dev,
  962. const struct i2c_reg_u8 *buf, int sz)
  963. {
  964. while (--sz >= 0) {
  965. i2c_w1(gspca_dev, buf->reg, buf->val);
  966. buf++;
  967. }
  968. }
  969. static void i2c_w2(struct gspca_dev *gspca_dev, u8 reg, u16 val)
  970. {
  971. struct sd *sd = (struct sd *) gspca_dev;
  972. u8 row[8];
  973. /*
  974. * from the point of view of the bridge, the length
  975. * includes the address
  976. */
  977. row[0] = sd->i2c_intf | (3 << 4);
  978. row[1] = sd->i2c_addr;
  979. row[2] = reg;
  980. row[3] = val >> 8;
  981. row[4] = val;
  982. row[5] = 0x00;
  983. row[6] = 0x00;
  984. row[7] = 0x10;
  985. i2c_w(gspca_dev, row);
  986. }
  987. static void i2c_w2_buf(struct gspca_dev *gspca_dev,
  988. const struct i2c_reg_u16 *buf, int sz)
  989. {
  990. while (--sz >= 0) {
  991. i2c_w2(gspca_dev, buf->reg, buf->val);
  992. buf++;
  993. }
  994. }
  995. static void i2c_r1(struct gspca_dev *gspca_dev, u8 reg, u8 *val)
  996. {
  997. struct sd *sd = (struct sd *) gspca_dev;
  998. u8 row[8];
  999. row[0] = sd->i2c_intf | (1 << 4);
  1000. row[1] = sd->i2c_addr;
  1001. row[2] = reg;
  1002. row[3] = 0;
  1003. row[4] = 0;
  1004. row[5] = 0;
  1005. row[6] = 0;
  1006. row[7] = 0x10;
  1007. i2c_w(gspca_dev, row);
  1008. row[0] = sd->i2c_intf | (1 << 4) | 0x02;
  1009. row[2] = 0;
  1010. i2c_w(gspca_dev, row);
  1011. reg_r(gspca_dev, 0x10c2, 5);
  1012. *val = gspca_dev->usb_buf[4];
  1013. }
  1014. static void i2c_r2(struct gspca_dev *gspca_dev, u8 reg, u16 *val)
  1015. {
  1016. struct sd *sd = (struct sd *) gspca_dev;
  1017. u8 row[8];
  1018. row[0] = sd->i2c_intf | (1 << 4);
  1019. row[1] = sd->i2c_addr;
  1020. row[2] = reg;
  1021. row[3] = 0;
  1022. row[4] = 0;
  1023. row[5] = 0;
  1024. row[6] = 0;
  1025. row[7] = 0x10;
  1026. i2c_w(gspca_dev, row);
  1027. row[0] = sd->i2c_intf | (2 << 4) | 0x02;
  1028. row[2] = 0;
  1029. i2c_w(gspca_dev, row);
  1030. reg_r(gspca_dev, 0x10c2, 5);
  1031. *val = (gspca_dev->usb_buf[3] << 8) | gspca_dev->usb_buf[4];
  1032. }
  1033. static void ov9650_init_sensor(struct gspca_dev *gspca_dev)
  1034. {
  1035. u16 id;
  1036. struct sd *sd = (struct sd *) gspca_dev;
  1037. i2c_r2(gspca_dev, 0x1c, &id);
  1038. if (gspca_dev->usb_err < 0)
  1039. return;
  1040. if (id != 0x7fa2) {
  1041. pr_err("sensor id for ov9650 doesn't match (0x%04x)\n", id);
  1042. gspca_dev->usb_err = -ENODEV;
  1043. return;
  1044. }
  1045. i2c_w1(gspca_dev, 0x12, 0x80); /* sensor reset */
  1046. msleep(200);
  1047. i2c_w1_buf(gspca_dev, ov9650_init, ARRAY_SIZE(ov9650_init));
  1048. if (gspca_dev->usb_err < 0)
  1049. pr_err("OV9650 sensor initialization failed\n");
  1050. sd->hstart = 1;
  1051. sd->vstart = 7;
  1052. }
  1053. static void ov9655_init_sensor(struct gspca_dev *gspca_dev)
  1054. {
  1055. struct sd *sd = (struct sd *) gspca_dev;
  1056. i2c_w1(gspca_dev, 0x12, 0x80); /* sensor reset */
  1057. msleep(200);
  1058. i2c_w1_buf(gspca_dev, ov9655_init, ARRAY_SIZE(ov9655_init));
  1059. if (gspca_dev->usb_err < 0)
  1060. pr_err("OV9655 sensor initialization failed\n");
  1061. sd->hstart = 1;
  1062. sd->vstart = 2;
  1063. }
  1064. static void soi968_init_sensor(struct gspca_dev *gspca_dev)
  1065. {
  1066. struct sd *sd = (struct sd *) gspca_dev;
  1067. i2c_w1(gspca_dev, 0x12, 0x80); /* sensor reset */
  1068. msleep(200);
  1069. i2c_w1_buf(gspca_dev, soi968_init, ARRAY_SIZE(soi968_init));
  1070. if (gspca_dev->usb_err < 0)
  1071. pr_err("SOI968 sensor initialization failed\n");
  1072. sd->hstart = 60;
  1073. sd->vstart = 11;
  1074. }
  1075. static void ov7660_init_sensor(struct gspca_dev *gspca_dev)
  1076. {
  1077. struct sd *sd = (struct sd *) gspca_dev;
  1078. i2c_w1(gspca_dev, 0x12, 0x80); /* sensor reset */
  1079. msleep(200);
  1080. i2c_w1_buf(gspca_dev, ov7660_init, ARRAY_SIZE(ov7660_init));
  1081. if (gspca_dev->usb_err < 0)
  1082. pr_err("OV7660 sensor initialization failed\n");
  1083. sd->hstart = 3;
  1084. sd->vstart = 3;
  1085. }
  1086. static void ov7670_init_sensor(struct gspca_dev *gspca_dev)
  1087. {
  1088. struct sd *sd = (struct sd *) gspca_dev;
  1089. i2c_w1(gspca_dev, 0x12, 0x80); /* sensor reset */
  1090. msleep(200);
  1091. i2c_w1_buf(gspca_dev, ov7670_init, ARRAY_SIZE(ov7670_init));
  1092. if (gspca_dev->usb_err < 0)
  1093. pr_err("OV7670 sensor initialization failed\n");
  1094. sd->hstart = 0;
  1095. sd->vstart = 1;
  1096. }
  1097. static void mt9v_init_sensor(struct gspca_dev *gspca_dev)
  1098. {
  1099. struct sd *sd = (struct sd *) gspca_dev;
  1100. u16 value;
  1101. sd->i2c_addr = 0x5d;
  1102. i2c_r2(gspca_dev, 0xff, &value);
  1103. if (gspca_dev->usb_err >= 0
  1104. && value == 0x8243) {
  1105. i2c_w2_buf(gspca_dev, mt9v011_init, ARRAY_SIZE(mt9v011_init));
  1106. if (gspca_dev->usb_err < 0) {
  1107. pr_err("MT9V011 sensor initialization failed\n");
  1108. return;
  1109. }
  1110. sd->hstart = 2;
  1111. sd->vstart = 2;
  1112. sd->sensor = SENSOR_MT9V011;
  1113. pr_info("MT9V011 sensor detected\n");
  1114. return;
  1115. }
  1116. gspca_dev->usb_err = 0;
  1117. sd->i2c_addr = 0x5c;
  1118. i2c_w2(gspca_dev, 0x01, 0x0004);
  1119. i2c_r2(gspca_dev, 0xff, &value);
  1120. if (gspca_dev->usb_err >= 0
  1121. && value == 0x823a) {
  1122. i2c_w2_buf(gspca_dev, mt9v111_init, ARRAY_SIZE(mt9v111_init));
  1123. if (gspca_dev->usb_err < 0) {
  1124. pr_err("MT9V111 sensor initialization failed\n");
  1125. return;
  1126. }
  1127. sd->hstart = 2;
  1128. sd->vstart = 2;
  1129. sd->sensor = SENSOR_MT9V111;
  1130. pr_info("MT9V111 sensor detected\n");
  1131. return;
  1132. }
  1133. gspca_dev->usb_err = 0;
  1134. sd->i2c_addr = 0x5d;
  1135. i2c_w2(gspca_dev, 0xf0, 0x0000);
  1136. if (gspca_dev->usb_err < 0) {
  1137. gspca_dev->usb_err = 0;
  1138. sd->i2c_addr = 0x48;
  1139. i2c_w2(gspca_dev, 0xf0, 0x0000);
  1140. }
  1141. i2c_r2(gspca_dev, 0x00, &value);
  1142. if (gspca_dev->usb_err >= 0
  1143. && value == 0x1229) {
  1144. i2c_w2_buf(gspca_dev, mt9v112_init, ARRAY_SIZE(mt9v112_init));
  1145. if (gspca_dev->usb_err < 0) {
  1146. pr_err("MT9V112 sensor initialization failed\n");
  1147. return;
  1148. }
  1149. sd->hstart = 6;
  1150. sd->vstart = 2;
  1151. sd->sensor = SENSOR_MT9V112;
  1152. pr_info("MT9V112 sensor detected\n");
  1153. return;
  1154. }
  1155. gspca_dev->usb_err = -ENODEV;
  1156. }
  1157. static void mt9m112_init_sensor(struct gspca_dev *gspca_dev)
  1158. {
  1159. struct sd *sd = (struct sd *) gspca_dev;
  1160. i2c_w2_buf(gspca_dev, mt9m112_init, ARRAY_SIZE(mt9m112_init));
  1161. if (gspca_dev->usb_err < 0)
  1162. pr_err("MT9M112 sensor initialization failed\n");
  1163. sd->hstart = 0;
  1164. sd->vstart = 2;
  1165. }
  1166. static void mt9m111_init_sensor(struct gspca_dev *gspca_dev)
  1167. {
  1168. struct sd *sd = (struct sd *) gspca_dev;
  1169. i2c_w2_buf(gspca_dev, mt9m111_init, ARRAY_SIZE(mt9m111_init));
  1170. if (gspca_dev->usb_err < 0)
  1171. pr_err("MT9M111 sensor initialization failed\n");
  1172. sd->hstart = 0;
  1173. sd->vstart = 2;
  1174. }
  1175. static void mt9m001_init_sensor(struct gspca_dev *gspca_dev)
  1176. {
  1177. struct sd *sd = (struct sd *) gspca_dev;
  1178. u16 id;
  1179. i2c_r2(gspca_dev, 0x00, &id);
  1180. if (gspca_dev->usb_err < 0)
  1181. return;
  1182. /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
  1183. switch (id) {
  1184. case 0x8411:
  1185. case 0x8421:
  1186. pr_info("MT9M001 color sensor detected\n");
  1187. break;
  1188. case 0x8431:
  1189. pr_info("MT9M001 mono sensor detected\n");
  1190. break;
  1191. default:
  1192. pr_err("No MT9M001 chip detected, ID = %x\n\n", id);
  1193. gspca_dev->usb_err = -ENODEV;
  1194. return;
  1195. }
  1196. i2c_w2_buf(gspca_dev, mt9m001_init, ARRAY_SIZE(mt9m001_init));
  1197. if (gspca_dev->usb_err < 0)
  1198. pr_err("MT9M001 sensor initialization failed\n");
  1199. sd->hstart = 1;
  1200. sd->vstart = 1;
  1201. }
  1202. static void hv7131r_init_sensor(struct gspca_dev *gspca_dev)
  1203. {
  1204. struct sd *sd = (struct sd *) gspca_dev;
  1205. i2c_w1_buf(gspca_dev, hv7131r_init, ARRAY_SIZE(hv7131r_init));
  1206. if (gspca_dev->usb_err < 0)
  1207. pr_err("HV7131R Sensor initialization failed\n");
  1208. sd->hstart = 0;
  1209. sd->vstart = 1;
  1210. }
  1211. static void set_cmatrix(struct gspca_dev *gspca_dev,
  1212. s32 brightness, s32 contrast, s32 satur, s32 hue)
  1213. {
  1214. s32 hue_coord, hue_index = 180 + hue;
  1215. u8 cmatrix[21];
  1216. memset(cmatrix, 0, sizeof cmatrix);
  1217. cmatrix[2] = (contrast * 0x25 / 0x100) + 0x26;
  1218. cmatrix[0] = 0x13 + (cmatrix[2] - 0x26) * 0x13 / 0x25;
  1219. cmatrix[4] = 0x07 + (cmatrix[2] - 0x26) * 0x07 / 0x25;
  1220. cmatrix[18] = brightness - 0x80;
  1221. hue_coord = (hsv_red_x[hue_index] * satur) >> 8;
  1222. cmatrix[6] = hue_coord;
  1223. cmatrix[7] = (hue_coord >> 8) & 0x0f;
  1224. hue_coord = (hsv_red_y[hue_index] * satur) >> 8;
  1225. cmatrix[8] = hue_coord;
  1226. cmatrix[9] = (hue_coord >> 8) & 0x0f;
  1227. hue_coord = (hsv_green_x[hue_index] * satur) >> 8;
  1228. cmatrix[10] = hue_coord;
  1229. cmatrix[11] = (hue_coord >> 8) & 0x0f;
  1230. hue_coord = (hsv_green_y[hue_index] * satur) >> 8;
  1231. cmatrix[12] = hue_coord;
  1232. cmatrix[13] = (hue_coord >> 8) & 0x0f;
  1233. hue_coord = (hsv_blue_x[hue_index] * satur) >> 8;
  1234. cmatrix[14] = hue_coord;
  1235. cmatrix[15] = (hue_coord >> 8) & 0x0f;
  1236. hue_coord = (hsv_blue_y[hue_index] * satur) >> 8;
  1237. cmatrix[16] = hue_coord;
  1238. cmatrix[17] = (hue_coord >> 8) & 0x0f;
  1239. reg_w(gspca_dev, 0x10e1, cmatrix, 21);
  1240. }
  1241. static void set_gamma(struct gspca_dev *gspca_dev, s32 val)
  1242. {
  1243. u8 gamma[17];
  1244. u8 gval = val * 0xb8 / 0x100;
  1245. gamma[0] = 0x0a;
  1246. gamma[1] = 0x13 + (gval * (0xcb - 0x13) / 0xb8);
  1247. gamma[2] = 0x25 + (gval * (0xee - 0x25) / 0xb8);
  1248. gamma[3] = 0x37 + (gval * (0xfa - 0x37) / 0xb8);
  1249. gamma[4] = 0x45 + (gval * (0xfc - 0x45) / 0xb8);
  1250. gamma[5] = 0x55 + (gval * (0xfb - 0x55) / 0xb8);
  1251. gamma[6] = 0x65 + (gval * (0xfc - 0x65) / 0xb8);
  1252. gamma[7] = 0x74 + (gval * (0xfd - 0x74) / 0xb8);
  1253. gamma[8] = 0x83 + (gval * (0xfe - 0x83) / 0xb8);
  1254. gamma[9] = 0x92 + (gval * (0xfc - 0x92) / 0xb8);
  1255. gamma[10] = 0xa1 + (gval * (0xfc - 0xa1) / 0xb8);
  1256. gamma[11] = 0xb0 + (gval * (0xfc - 0xb0) / 0xb8);
  1257. gamma[12] = 0xbf + (gval * (0xfb - 0xbf) / 0xb8);
  1258. gamma[13] = 0xce + (gval * (0xfb - 0xce) / 0xb8);
  1259. gamma[14] = 0xdf + (gval * (0xfd - 0xdf) / 0xb8);
  1260. gamma[15] = 0xea + (gval * (0xf9 - 0xea) / 0xb8);
  1261. gamma[16] = 0xf5;
  1262. reg_w(gspca_dev, 0x1190, gamma, 17);
  1263. }
  1264. static void set_redblue(struct gspca_dev *gspca_dev, s32 blue, s32 red)
  1265. {
  1266. reg_w1(gspca_dev, 0x118c, red);
  1267. reg_w1(gspca_dev, 0x118f, blue);
  1268. }
  1269. static void set_hvflip(struct gspca_dev *gspca_dev, s32 hflip, s32 vflip)
  1270. {
  1271. u8 value, tslb;
  1272. u16 value2;
  1273. struct sd *sd = (struct sd *) gspca_dev;
  1274. if ((sd->flags & FLIP_DETECT) && dmi_check_system(flip_dmi_table)) {
  1275. hflip = !hflip;
  1276. vflip = !vflip;
  1277. }
  1278. switch (sd->sensor) {
  1279. case SENSOR_OV7660:
  1280. value = 0x01;
  1281. if (hflip)
  1282. value |= 0x20;
  1283. if (vflip) {
  1284. value |= 0x10;
  1285. sd->vstart = 2;
  1286. } else {
  1287. sd->vstart = 3;
  1288. }
  1289. reg_w1(gspca_dev, 0x1182, sd->vstart);
  1290. i2c_w1(gspca_dev, 0x1e, value);
  1291. break;
  1292. case SENSOR_OV9650:
  1293. i2c_r1(gspca_dev, 0x1e, &value);
  1294. value &= ~0x30;
  1295. tslb = 0x01;
  1296. if (hflip)
  1297. value |= 0x20;
  1298. if (vflip) {
  1299. value |= 0x10;
  1300. tslb = 0x49;
  1301. }
  1302. i2c_w1(gspca_dev, 0x1e, value);
  1303. i2c_w1(gspca_dev, 0x3a, tslb);
  1304. break;
  1305. case SENSOR_MT9V111:
  1306. case SENSOR_MT9V011:
  1307. i2c_r2(gspca_dev, 0x20, &value2);
  1308. value2 &= ~0xc0a0;
  1309. if (hflip)
  1310. value2 |= 0x8080;
  1311. if (vflip)
  1312. value2 |= 0x4020;
  1313. i2c_w2(gspca_dev, 0x20, value2);
  1314. break;
  1315. case SENSOR_MT9M112:
  1316. case SENSOR_MT9M111:
  1317. case SENSOR_MT9V112:
  1318. i2c_r2(gspca_dev, 0x20, &value2);
  1319. value2 &= ~0x0003;
  1320. if (hflip)
  1321. value2 |= 0x0002;
  1322. if (vflip)
  1323. value2 |= 0x0001;
  1324. i2c_w2(gspca_dev, 0x20, value2);
  1325. break;
  1326. case SENSOR_HV7131R:
  1327. i2c_r1(gspca_dev, 0x01, &value);
  1328. value &= ~0x03;
  1329. if (vflip)
  1330. value |= 0x01;
  1331. if (hflip)
  1332. value |= 0x02;
  1333. i2c_w1(gspca_dev, 0x01, value);
  1334. break;
  1335. }
  1336. }
  1337. static void set_exposure(struct gspca_dev *gspca_dev, s32 expo)
  1338. {
  1339. struct sd *sd = (struct sd *) gspca_dev;
  1340. u8 exp[8] = {sd->i2c_intf, sd->i2c_addr,
  1341. 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
  1342. int expo2;
  1343. if (gspca_dev->streaming)
  1344. exp[7] = 0x1e;
  1345. switch (sd->sensor) {
  1346. case SENSOR_OV7660:
  1347. case SENSOR_OV7670:
  1348. case SENSOR_OV9655:
  1349. case SENSOR_OV9650:
  1350. if (expo > 547)
  1351. expo2 = 547;
  1352. else
  1353. expo2 = expo;
  1354. exp[0] |= (2 << 4);
  1355. exp[2] = 0x10; /* AECH */
  1356. exp[3] = expo2 >> 2;
  1357. exp[7] = 0x10;
  1358. i2c_w(gspca_dev, exp);
  1359. exp[2] = 0x04; /* COM1 */
  1360. exp[3] = expo2 & 0x0003;
  1361. exp[7] = 0x10;
  1362. i2c_w(gspca_dev, exp);
  1363. expo -= expo2;
  1364. exp[7] = 0x1e;
  1365. exp[0] |= (3 << 4);
  1366. exp[2] = 0x2d; /* ADVFL & ADVFH */
  1367. exp[3] = expo;
  1368. exp[4] = expo >> 8;
  1369. break;
  1370. case SENSOR_MT9M001:
  1371. case SENSOR_MT9V112:
  1372. case SENSOR_MT9V011:
  1373. exp[0] |= (3 << 4);
  1374. exp[2] = 0x09;
  1375. exp[3] = expo >> 8;
  1376. exp[4] = expo;
  1377. break;
  1378. case SENSOR_HV7131R:
  1379. exp[0] |= (4 << 4);
  1380. exp[2] = 0x25;
  1381. exp[3] = expo >> 5;
  1382. exp[4] = expo << 3;
  1383. exp[5] = 0;
  1384. break;
  1385. default:
  1386. return;
  1387. }
  1388. i2c_w(gspca_dev, exp);
  1389. }
  1390. static void set_gain(struct gspca_dev *gspca_dev, s32 g)
  1391. {
  1392. struct sd *sd = (struct sd *) gspca_dev;
  1393. u8 gain[8] = {sd->i2c_intf, sd->i2c_addr,
  1394. 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
  1395. if (gspca_dev->streaming)
  1396. gain[7] = 0x15; /* or 1d ? */
  1397. switch (sd->sensor) {
  1398. case SENSOR_OV7660:
  1399. case SENSOR_OV7670:
  1400. case SENSOR_SOI968:
  1401. case SENSOR_OV9655:
  1402. case SENSOR_OV9650:
  1403. gain[0] |= (2 << 4);
  1404. gain[3] = ov_gain[g];
  1405. break;
  1406. case SENSOR_MT9V011:
  1407. gain[0] |= (3 << 4);
  1408. gain[2] = 0x35;
  1409. gain[3] = micron1_gain[g] >> 8;
  1410. gain[4] = micron1_gain[g];
  1411. break;
  1412. case SENSOR_MT9V112:
  1413. gain[0] |= (3 << 4);
  1414. gain[2] = 0x2f;
  1415. gain[3] = micron1_gain[g] >> 8;
  1416. gain[4] = micron1_gain[g];
  1417. break;
  1418. case SENSOR_MT9M001:
  1419. gain[0] |= (3 << 4);
  1420. gain[2] = 0x2f;
  1421. gain[3] = micron2_gain[g] >> 8;
  1422. gain[4] = micron2_gain[g];
  1423. break;
  1424. case SENSOR_HV7131R:
  1425. gain[0] |= (2 << 4);
  1426. gain[2] = 0x30;
  1427. gain[3] = hv7131r_gain[g];
  1428. break;
  1429. default:
  1430. return;
  1431. }
  1432. i2c_w(gspca_dev, gain);
  1433. }
  1434. static void set_quality(struct gspca_dev *gspca_dev, s32 val)
  1435. {
  1436. struct sd *sd = (struct sd *) gspca_dev;
  1437. jpeg_set_qual(sd->jpeg_hdr, val);
  1438. reg_w1(gspca_dev, 0x1061, 0x01); /* stop transfer */
  1439. reg_w1(gspca_dev, 0x10e0, sd->fmt | 0x20); /* write QTAB */
  1440. reg_w(gspca_dev, 0x1100, &sd->jpeg_hdr[JPEG_QT0_OFFSET], 64);
  1441. reg_w(gspca_dev, 0x1140, &sd->jpeg_hdr[JPEG_QT1_OFFSET], 64);
  1442. reg_w1(gspca_dev, 0x1061, 0x03); /* restart transfer */
  1443. reg_w1(gspca_dev, 0x10e0, sd->fmt);
  1444. sd->fmt ^= 0x0c; /* invert QTAB use + write */
  1445. reg_w1(gspca_dev, 0x10e0, sd->fmt);
  1446. }
  1447. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1448. static int sd_dbg_g_register(struct gspca_dev *gspca_dev,
  1449. struct v4l2_dbg_register *reg)
  1450. {
  1451. struct sd *sd = (struct sd *) gspca_dev;
  1452. switch (reg->match.type) {
  1453. case V4L2_CHIP_MATCH_HOST:
  1454. if (reg->match.addr != 0)
  1455. return -EINVAL;
  1456. if (reg->reg < 0x1000 || reg->reg > 0x11ff)
  1457. return -EINVAL;
  1458. reg_r(gspca_dev, reg->reg, 1);
  1459. reg->val = gspca_dev->usb_buf[0];
  1460. return gspca_dev->usb_err;
  1461. case V4L2_CHIP_MATCH_I2C_ADDR:
  1462. if (reg->match.addr != sd->i2c_addr)
  1463. return -EINVAL;
  1464. if (sd->sensor >= SENSOR_MT9V011 &&
  1465. sd->sensor <= SENSOR_MT9M112) {
  1466. i2c_r2(gspca_dev, reg->reg, (u16 *) &reg->val);
  1467. } else {
  1468. i2c_r1(gspca_dev, reg->reg, (u8 *) &reg->val);
  1469. }
  1470. return gspca_dev->usb_err;
  1471. }
  1472. return -EINVAL;
  1473. }
  1474. static int sd_dbg_s_register(struct gspca_dev *gspca_dev,
  1475. struct v4l2_dbg_register *reg)
  1476. {
  1477. struct sd *sd = (struct sd *) gspca_dev;
  1478. switch (reg->match.type) {
  1479. case V4L2_CHIP_MATCH_HOST:
  1480. if (reg->match.addr != 0)
  1481. return -EINVAL;
  1482. if (reg->reg < 0x1000 || reg->reg > 0x11ff)
  1483. return -EINVAL;
  1484. reg_w1(gspca_dev, reg->reg, reg->val);
  1485. return gspca_dev->usb_err;
  1486. case V4L2_CHIP_MATCH_I2C_ADDR:
  1487. if (reg->match.addr != sd->i2c_addr)
  1488. return -EINVAL;
  1489. if (sd->sensor >= SENSOR_MT9V011 &&
  1490. sd->sensor <= SENSOR_MT9M112) {
  1491. i2c_w2(gspca_dev, reg->reg, reg->val);
  1492. } else {
  1493. i2c_w1(gspca_dev, reg->reg, reg->val);
  1494. }
  1495. return gspca_dev->usb_err;
  1496. }
  1497. return -EINVAL;
  1498. }
  1499. #endif
  1500. static int sd_chip_ident(struct gspca_dev *gspca_dev,
  1501. struct v4l2_dbg_chip_ident *chip)
  1502. {
  1503. struct sd *sd = (struct sd *) gspca_dev;
  1504. switch (chip->match.type) {
  1505. case V4L2_CHIP_MATCH_HOST:
  1506. if (chip->match.addr != 0)
  1507. return -EINVAL;
  1508. chip->revision = 0;
  1509. chip->ident = V4L2_IDENT_SN9C20X;
  1510. return 0;
  1511. case V4L2_CHIP_MATCH_I2C_ADDR:
  1512. if (chip->match.addr != sd->i2c_addr)
  1513. return -EINVAL;
  1514. chip->revision = 0;
  1515. chip->ident = i2c_ident[sd->sensor];
  1516. return 0;
  1517. }
  1518. return -EINVAL;
  1519. }
  1520. static int sd_config(struct gspca_dev *gspca_dev,
  1521. const struct usb_device_id *id)
  1522. {
  1523. struct sd *sd = (struct sd *) gspca_dev;
  1524. struct cam *cam;
  1525. cam = &gspca_dev->cam;
  1526. cam->needs_full_bandwidth = 1;
  1527. sd->sensor = id->driver_info >> 8;
  1528. sd->i2c_addr = id->driver_info;
  1529. sd->flags = id->driver_info >> 16;
  1530. sd->i2c_intf = 0x80; /* i2c 100 Kb/s */
  1531. switch (sd->sensor) {
  1532. case SENSOR_MT9M112:
  1533. case SENSOR_MT9M111:
  1534. case SENSOR_OV9650:
  1535. case SENSOR_SOI968:
  1536. cam->cam_mode = sxga_mode;
  1537. cam->nmodes = ARRAY_SIZE(sxga_mode);
  1538. break;
  1539. case SENSOR_MT9M001:
  1540. cam->cam_mode = mono_mode;
  1541. cam->nmodes = ARRAY_SIZE(mono_mode);
  1542. break;
  1543. case SENSOR_HV7131R:
  1544. sd->i2c_intf = 0x81; /* i2c 400 Kb/s */
  1545. /* fall thru */
  1546. default:
  1547. cam->cam_mode = vga_mode;
  1548. cam->nmodes = ARRAY_SIZE(vga_mode);
  1549. break;
  1550. }
  1551. sd->old_step = 0;
  1552. sd->older_step = 0;
  1553. sd->exposure_step = 16;
  1554. INIT_WORK(&sd->work, qual_upd);
  1555. return 0;
  1556. }
  1557. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  1558. {
  1559. struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
  1560. struct gspca_dev *gspca_dev = &sd->gspca_dev;
  1561. gspca_dev->usb_err = 0;
  1562. if (!gspca_dev->streaming)
  1563. return 0;
  1564. switch (ctrl->id) {
  1565. /* color control cluster */
  1566. case V4L2_CID_BRIGHTNESS:
  1567. set_cmatrix(&sd->gspca_dev, sd->brightness->val,
  1568. sd->contrast->val, sd->saturation->val, sd->hue->val);
  1569. break;
  1570. case V4L2_CID_GAMMA:
  1571. set_gamma(&sd->gspca_dev, ctrl->val);
  1572. break;
  1573. /* blue/red balance cluster */
  1574. case V4L2_CID_BLUE_BALANCE:
  1575. set_redblue(&sd->gspca_dev, sd->blue->val, sd->red->val);
  1576. break;
  1577. /* h/vflip cluster */
  1578. case V4L2_CID_HFLIP:
  1579. set_hvflip(&sd->gspca_dev, sd->hflip->val, sd->vflip->val);
  1580. break;
  1581. /* standalone exposure control */
  1582. case V4L2_CID_EXPOSURE:
  1583. set_exposure(&sd->gspca_dev, ctrl->val);
  1584. break;
  1585. /* standalone gain control */
  1586. case V4L2_CID_GAIN:
  1587. set_gain(&sd->gspca_dev, ctrl->val);
  1588. break;
  1589. /* autogain + exposure or gain control cluster */
  1590. case V4L2_CID_AUTOGAIN:
  1591. if (sd->sensor == SENSOR_SOI968)
  1592. set_gain(&sd->gspca_dev, sd->gain->val);
  1593. else
  1594. set_exposure(&sd->gspca_dev, sd->exposure->val);
  1595. break;
  1596. case V4L2_CID_JPEG_COMPRESSION_QUALITY:
  1597. set_quality(&sd->gspca_dev, ctrl->val);
  1598. break;
  1599. }
  1600. return gspca_dev->usb_err;
  1601. }
  1602. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  1603. .s_ctrl = sd_s_ctrl,
  1604. };
  1605. static int sd_init_controls(struct gspca_dev *gspca_dev)
  1606. {
  1607. struct sd *sd = (struct sd *) gspca_dev;
  1608. struct v4l2_ctrl_handler *hdl = &sd->ctrl_handler;
  1609. gspca_dev->vdev.ctrl_handler = hdl;
  1610. v4l2_ctrl_handler_init(hdl, 13);
  1611. sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1612. V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
  1613. sd->contrast = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1614. V4L2_CID_CONTRAST, 0, 255, 1, 127);
  1615. sd->saturation = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1616. V4L2_CID_SATURATION, 0, 255, 1, 127);
  1617. sd->hue = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1618. V4L2_CID_HUE, -180, 180, 1, 0);
  1619. v4l2_ctrl_cluster(4, &sd->brightness);
  1620. sd->gamma = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1621. V4L2_CID_GAMMA, 0, 255, 1, 0x10);
  1622. sd->blue = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1623. V4L2_CID_BLUE_BALANCE, 0, 127, 1, 0x28);
  1624. sd->red = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1625. V4L2_CID_RED_BALANCE, 0, 127, 1, 0x28);
  1626. v4l2_ctrl_cluster(2, &sd->blue);
  1627. if (sd->sensor != SENSOR_OV9655 && sd->sensor != SENSOR_SOI968 &&
  1628. sd->sensor != SENSOR_OV7670 && sd->sensor != SENSOR_MT9M001 &&
  1629. sd->sensor != SENSOR_MT9VPRB) {
  1630. sd->hflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1631. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1632. sd->vflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1633. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1634. v4l2_ctrl_cluster(2, &sd->hflip);
  1635. }
  1636. if (sd->sensor != SENSOR_SOI968 && sd->sensor != SENSOR_MT9VPRB &&
  1637. sd->sensor != SENSOR_MT9M112 && sd->sensor != SENSOR_MT9M111 &&
  1638. sd->sensor != SENSOR_MT9V111)
  1639. sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1640. V4L2_CID_EXPOSURE, 0, 0x1780, 1, 0x33);
  1641. if (sd->sensor != SENSOR_MT9VPRB && sd->sensor != SENSOR_MT9M112 &&
  1642. sd->sensor != SENSOR_MT9M111 && sd->sensor != SENSOR_MT9V111) {
  1643. sd->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1644. V4L2_CID_GAIN, 0, 28, 1, 0);
  1645. sd->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1646. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  1647. if (sd->sensor == SENSOR_SOI968)
  1648. /* this sensor doesn't have the exposure control and
  1649. autogain is clustered with gain instead. This works
  1650. because sd->exposure == NULL. */
  1651. v4l2_ctrl_auto_cluster(3, &sd->autogain, 0, false);
  1652. else
  1653. /* Otherwise autogain is clustered with exposure. */
  1654. v4l2_ctrl_auto_cluster(2, &sd->autogain, 0, false);
  1655. }
  1656. sd->jpegqual = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1657. V4L2_CID_JPEG_COMPRESSION_QUALITY, 50, 90, 1, 80);
  1658. if (hdl->error) {
  1659. pr_err("Could not initialize controls\n");
  1660. return hdl->error;
  1661. }
  1662. return 0;
  1663. }
  1664. static int sd_init(struct gspca_dev *gspca_dev)
  1665. {
  1666. struct sd *sd = (struct sd *) gspca_dev;
  1667. int i;
  1668. u8 value;
  1669. u8 i2c_init[9] =
  1670. {0x80, sd->i2c_addr, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03};
  1671. for (i = 0; i < ARRAY_SIZE(bridge_init); i++) {
  1672. value = bridge_init[i][1];
  1673. reg_w(gspca_dev, bridge_init[i][0], &value, 1);
  1674. if (gspca_dev->usb_err < 0) {
  1675. pr_err("Device initialization failed\n");
  1676. return gspca_dev->usb_err;
  1677. }
  1678. }
  1679. if (sd->flags & LED_REVERSE)
  1680. reg_w1(gspca_dev, 0x1006, 0x00);
  1681. else
  1682. reg_w1(gspca_dev, 0x1006, 0x20);
  1683. reg_w(gspca_dev, 0x10c0, i2c_init, 9);
  1684. if (gspca_dev->usb_err < 0) {
  1685. pr_err("Device initialization failed\n");
  1686. return gspca_dev->usb_err;
  1687. }
  1688. switch (sd->sensor) {
  1689. case SENSOR_OV9650:
  1690. ov9650_init_sensor(gspca_dev);
  1691. if (gspca_dev->usb_err < 0)
  1692. break;
  1693. pr_info("OV9650 sensor detected\n");
  1694. break;
  1695. case SENSOR_OV9655:
  1696. ov9655_init_sensor(gspca_dev);
  1697. if (gspca_dev->usb_err < 0)
  1698. break;
  1699. pr_info("OV9655 sensor detected\n");
  1700. break;
  1701. case SENSOR_SOI968:
  1702. soi968_init_sensor(gspca_dev);
  1703. if (gspca_dev->usb_err < 0)
  1704. break;
  1705. pr_info("SOI968 sensor detected\n");
  1706. break;
  1707. case SENSOR_OV7660:
  1708. ov7660_init_sensor(gspca_dev);
  1709. if (gspca_dev->usb_err < 0)
  1710. break;
  1711. pr_info("OV7660 sensor detected\n");
  1712. break;
  1713. case SENSOR_OV7670:
  1714. ov7670_init_sensor(gspca_dev);
  1715. if (gspca_dev->usb_err < 0)
  1716. break;
  1717. pr_info("OV7670 sensor detected\n");
  1718. break;
  1719. case SENSOR_MT9VPRB:
  1720. mt9v_init_sensor(gspca_dev);
  1721. if (gspca_dev->usb_err < 0)
  1722. break;
  1723. pr_info("MT9VPRB sensor detected\n");
  1724. break;
  1725. case SENSOR_MT9M111:
  1726. mt9m111_init_sensor(gspca_dev);
  1727. if (gspca_dev->usb_err < 0)
  1728. break;
  1729. pr_info("MT9M111 sensor detected\n");
  1730. break;
  1731. case SENSOR_MT9M112:
  1732. mt9m112_init_sensor(gspca_dev);
  1733. if (gspca_dev->usb_err < 0)
  1734. break;
  1735. pr_info("MT9M112 sensor detected\n");
  1736. break;
  1737. case SENSOR_MT9M001:
  1738. mt9m001_init_sensor(gspca_dev);
  1739. if (gspca_dev->usb_err < 0)
  1740. break;
  1741. break;
  1742. case SENSOR_HV7131R:
  1743. hv7131r_init_sensor(gspca_dev);
  1744. if (gspca_dev->usb_err < 0)
  1745. break;
  1746. pr_info("HV7131R sensor detected\n");
  1747. break;
  1748. default:
  1749. pr_err("Unsupported sensor\n");
  1750. gspca_dev->usb_err = -ENODEV;
  1751. }
  1752. return gspca_dev->usb_err;
  1753. }
  1754. static void configure_sensor_output(struct gspca_dev *gspca_dev, int mode)
  1755. {
  1756. struct sd *sd = (struct sd *) gspca_dev;
  1757. u8 value;
  1758. switch (sd->sensor) {
  1759. case SENSOR_SOI968:
  1760. if (mode & MODE_SXGA) {
  1761. i2c_w1(gspca_dev, 0x17, 0x1d);
  1762. i2c_w1(gspca_dev, 0x18, 0xbd);
  1763. i2c_w1(gspca_dev, 0x19, 0x01);
  1764. i2c_w1(gspca_dev, 0x1a, 0x81);
  1765. i2c_w1(gspca_dev, 0x12, 0x00);
  1766. sd->hstart = 140;
  1767. sd->vstart = 19;
  1768. } else {
  1769. i2c_w1(gspca_dev, 0x17, 0x13);
  1770. i2c_w1(gspca_dev, 0x18, 0x63);
  1771. i2c_w1(gspca_dev, 0x19, 0x01);
  1772. i2c_w1(gspca_dev, 0x1a, 0x79);
  1773. i2c_w1(gspca_dev, 0x12, 0x40);
  1774. sd->hstart = 60;
  1775. sd->vstart = 11;
  1776. }
  1777. break;
  1778. case SENSOR_OV9650:
  1779. if (mode & MODE_SXGA) {
  1780. i2c_w1(gspca_dev, 0x17, 0x1b);
  1781. i2c_w1(gspca_dev, 0x18, 0xbc);
  1782. i2c_w1(gspca_dev, 0x19, 0x01);
  1783. i2c_w1(gspca_dev, 0x1a, 0x82);
  1784. i2c_r1(gspca_dev, 0x12, &value);
  1785. i2c_w1(gspca_dev, 0x12, value & 0x07);
  1786. } else {
  1787. i2c_w1(gspca_dev, 0x17, 0x24);
  1788. i2c_w1(gspca_dev, 0x18, 0xc5);
  1789. i2c_w1(gspca_dev, 0x19, 0x00);
  1790. i2c_w1(gspca_dev, 0x1a, 0x3c);
  1791. i2c_r1(gspca_dev, 0x12, &value);
  1792. i2c_w1(gspca_dev, 0x12, (value & 0x7) | 0x40);
  1793. }
  1794. break;
  1795. case SENSOR_MT9M112:
  1796. case SENSOR_MT9M111:
  1797. if (mode & MODE_SXGA) {
  1798. i2c_w2(gspca_dev, 0xf0, 0x0002);
  1799. i2c_w2(gspca_dev, 0xc8, 0x970b);
  1800. i2c_w2(gspca_dev, 0xf0, 0x0000);
  1801. } else {
  1802. i2c_w2(gspca_dev, 0xf0, 0x0002);
  1803. i2c_w2(gspca_dev, 0xc8, 0x8000);
  1804. i2c_w2(gspca_dev, 0xf0, 0x0000);
  1805. }
  1806. break;
  1807. }
  1808. }
  1809. static int sd_isoc_init(struct gspca_dev *gspca_dev)
  1810. {
  1811. struct usb_interface *intf;
  1812. u32 flags = gspca_dev->cam.cam_mode[(int)gspca_dev->curr_mode].priv;
  1813. /*
  1814. * When using the SN9C20X_I420 fmt the sn9c20x needs more bandwidth
  1815. * than our regular bandwidth calculations reserve, so we force the
  1816. * use of a specific altsetting when using the SN9C20X_I420 fmt.
  1817. */
  1818. if (!(flags & (MODE_RAW | MODE_JPEG))) {
  1819. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  1820. if (intf->num_altsetting != 9) {
  1821. pr_warn("sn9c20x camera with unknown number of alt "
  1822. "settings (%d), please report!\n",
  1823. intf->num_altsetting);
  1824. gspca_dev->alt = intf->num_altsetting;
  1825. return 0;
  1826. }
  1827. switch (gspca_dev->width) {
  1828. case 160: /* 160x120 */
  1829. gspca_dev->alt = 2;
  1830. break;
  1831. case 320: /* 320x240 */
  1832. gspca_dev->alt = 6;
  1833. break;
  1834. default: /* >= 640x480 */
  1835. gspca_dev->alt = 9;
  1836. break;
  1837. }
  1838. }
  1839. return 0;
  1840. }
  1841. #define HW_WIN(mode, hstart, vstart) \
  1842. ((const u8 []){hstart, 0, vstart, 0, \
  1843. (mode & MODE_SXGA ? 1280 >> 4 : 640 >> 4), \
  1844. (mode & MODE_SXGA ? 1024 >> 3 : 480 >> 3)})
  1845. #define CLR_WIN(width, height) \
  1846. ((const u8 [])\
  1847. {0, width >> 2, 0, height >> 1,\
  1848. ((width >> 10) & 0x01) | ((height >> 8) & 0x6)})
  1849. static int sd_start(struct gspca_dev *gspca_dev)
  1850. {
  1851. struct sd *sd = (struct sd *) gspca_dev;
  1852. int mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
  1853. int width = gspca_dev->width;
  1854. int height = gspca_dev->height;
  1855. u8 fmt, scale = 0;
  1856. jpeg_define(sd->jpeg_hdr, height, width,
  1857. 0x21);
  1858. jpeg_set_qual(sd->jpeg_hdr, v4l2_ctrl_g_ctrl(sd->jpegqual));
  1859. if (mode & MODE_RAW)
  1860. fmt = 0x2d;
  1861. else if (mode & MODE_JPEG)
  1862. fmt = 0x24;
  1863. else
  1864. fmt = 0x2f; /* YUV 420 */
  1865. sd->fmt = fmt;
  1866. switch (mode & SCALE_MASK) {
  1867. case SCALE_1280x1024:
  1868. scale = 0xc0;
  1869. pr_info("Set 1280x1024\n");
  1870. break;
  1871. case SCALE_640x480:
  1872. scale = 0x80;
  1873. pr_info("Set 640x480\n");
  1874. break;
  1875. case SCALE_320x240:
  1876. scale = 0x90;
  1877. pr_info("Set 320x240\n");
  1878. break;
  1879. case SCALE_160x120:
  1880. scale = 0xa0;
  1881. pr_info("Set 160x120\n");
  1882. break;
  1883. }
  1884. configure_sensor_output(gspca_dev, mode);
  1885. reg_w(gspca_dev, 0x1100, &sd->jpeg_hdr[JPEG_QT0_OFFSET], 64);
  1886. reg_w(gspca_dev, 0x1140, &sd->jpeg_hdr[JPEG_QT1_OFFSET], 64);
  1887. reg_w(gspca_dev, 0x10fb, CLR_WIN(width, height), 5);
  1888. reg_w(gspca_dev, 0x1180, HW_WIN(mode, sd->hstart, sd->vstart), 6);
  1889. reg_w1(gspca_dev, 0x1189, scale);
  1890. reg_w1(gspca_dev, 0x10e0, fmt);
  1891. set_cmatrix(gspca_dev, v4l2_ctrl_g_ctrl(sd->brightness),
  1892. v4l2_ctrl_g_ctrl(sd->contrast),
  1893. v4l2_ctrl_g_ctrl(sd->saturation),
  1894. v4l2_ctrl_g_ctrl(sd->hue));
  1895. set_gamma(gspca_dev, v4l2_ctrl_g_ctrl(sd->gamma));
  1896. set_redblue(gspca_dev, v4l2_ctrl_g_ctrl(sd->blue),
  1897. v4l2_ctrl_g_ctrl(sd->red));
  1898. set_gain(gspca_dev, v4l2_ctrl_g_ctrl(sd->gain));
  1899. set_exposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure));
  1900. set_hvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip),
  1901. v4l2_ctrl_g_ctrl(sd->vflip));
  1902. reg_w1(gspca_dev, 0x1007, 0x20);
  1903. reg_w1(gspca_dev, 0x1061, 0x03);
  1904. /* if JPEG, prepare the compression quality update */
  1905. if (mode & MODE_JPEG) {
  1906. sd->pktsz = sd->npkt = 0;
  1907. sd->nchg = 0;
  1908. sd->work_thread =
  1909. create_singlethread_workqueue(KBUILD_MODNAME);
  1910. }
  1911. return gspca_dev->usb_err;
  1912. }
  1913. static void sd_stopN(struct gspca_dev *gspca_dev)
  1914. {
  1915. reg_w1(gspca_dev, 0x1007, 0x00);
  1916. reg_w1(gspca_dev, 0x1061, 0x01);
  1917. }
  1918. /* called on streamoff with alt==0 and on disconnect */
  1919. /* the usb_lock is held at entry - restore on exit */
  1920. static void sd_stop0(struct gspca_dev *gspca_dev)
  1921. {
  1922. struct sd *sd = (struct sd *) gspca_dev;
  1923. if (sd->work_thread != NULL) {
  1924. mutex_unlock(&gspca_dev->usb_lock);
  1925. destroy_workqueue(sd->work_thread);
  1926. mutex_lock(&gspca_dev->usb_lock);
  1927. sd->work_thread = NULL;
  1928. }
  1929. }
  1930. static void do_autoexposure(struct gspca_dev *gspca_dev, u16 avg_lum)
  1931. {
  1932. struct sd *sd = (struct sd *) gspca_dev;
  1933. s32 cur_exp = v4l2_ctrl_g_ctrl(sd->exposure);
  1934. s32 max = sd->exposure->maximum - sd->exposure_step;
  1935. s32 min = sd->exposure->minimum + sd->exposure_step;
  1936. s16 new_exp;
  1937. /*
  1938. * some hardcoded values are present
  1939. * like those for maximal/minimal exposure
  1940. * and exposure steps
  1941. */
  1942. if (avg_lum < MIN_AVG_LUM) {
  1943. if (cur_exp > max)
  1944. return;
  1945. new_exp = cur_exp + sd->exposure_step;
  1946. if (new_exp > max)
  1947. new_exp = max;
  1948. if (new_exp < min)
  1949. new_exp = min;
  1950. v4l2_ctrl_s_ctrl(sd->exposure, new_exp);
  1951. sd->older_step = sd->old_step;
  1952. sd->old_step = 1;
  1953. if (sd->old_step ^ sd->older_step)
  1954. sd->exposure_step /= 2;
  1955. else
  1956. sd->exposure_step += 2;
  1957. }
  1958. if (avg_lum > MAX_AVG_LUM) {
  1959. if (cur_exp < min)
  1960. return;
  1961. new_exp = cur_exp - sd->exposure_step;
  1962. if (new_exp > max)
  1963. new_exp = max;
  1964. if (new_exp < min)
  1965. new_exp = min;
  1966. v4l2_ctrl_s_ctrl(sd->exposure, new_exp);
  1967. sd->older_step = sd->old_step;
  1968. sd->old_step = 0;
  1969. if (sd->old_step ^ sd->older_step)
  1970. sd->exposure_step /= 2;
  1971. else
  1972. sd->exposure_step += 2;
  1973. }
  1974. }
  1975. static void do_autogain(struct gspca_dev *gspca_dev, u16 avg_lum)
  1976. {
  1977. struct sd *sd = (struct sd *) gspca_dev;
  1978. s32 cur_gain = v4l2_ctrl_g_ctrl(sd->gain);
  1979. if (avg_lum < MIN_AVG_LUM && cur_gain < sd->gain->maximum)
  1980. v4l2_ctrl_s_ctrl(sd->gain, cur_gain + 1);
  1981. if (avg_lum > MAX_AVG_LUM && cur_gain > sd->gain->minimum)
  1982. v4l2_ctrl_s_ctrl(sd->gain, cur_gain - 1);
  1983. }
  1984. static void sd_dqcallback(struct gspca_dev *gspca_dev)
  1985. {
  1986. struct sd *sd = (struct sd *) gspca_dev;
  1987. int avg_lum;
  1988. if (!v4l2_ctrl_g_ctrl(sd->autogain))
  1989. return;
  1990. avg_lum = atomic_read(&sd->avg_lum);
  1991. if (sd->sensor == SENSOR_SOI968)
  1992. do_autogain(gspca_dev, avg_lum);
  1993. else
  1994. do_autoexposure(gspca_dev, avg_lum);
  1995. }
  1996. /* JPEG quality update */
  1997. /* This function is executed from a work queue. */
  1998. static void qual_upd(struct work_struct *work)
  1999. {
  2000. struct sd *sd = container_of(work, struct sd, work);
  2001. struct gspca_dev *gspca_dev = &sd->gspca_dev;
  2002. s32 qual = v4l2_ctrl_g_ctrl(sd->jpegqual);
  2003. mutex_lock(&gspca_dev->usb_lock);
  2004. PDEBUG(D_STREAM, "qual_upd %d%%", qual);
  2005. set_quality(gspca_dev, qual);
  2006. mutex_unlock(&gspca_dev->usb_lock);
  2007. }
  2008. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2009. static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
  2010. u8 *data, /* interrupt packet */
  2011. int len) /* interrupt packet length */
  2012. {
  2013. struct sd *sd = (struct sd *) gspca_dev;
  2014. if (!(sd->flags & HAS_NO_BUTTON) && len == 1) {
  2015. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
  2016. input_sync(gspca_dev->input_dev);
  2017. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
  2018. input_sync(gspca_dev->input_dev);
  2019. return 0;
  2020. }
  2021. return -EINVAL;
  2022. }
  2023. #endif
  2024. /* check the JPEG compression */
  2025. static void transfer_check(struct gspca_dev *gspca_dev,
  2026. u8 *data)
  2027. {
  2028. struct sd *sd = (struct sd *) gspca_dev;
  2029. int new_qual, r;
  2030. new_qual = 0;
  2031. /* if USB error, discard the frame and decrease the quality */
  2032. if (data[6] & 0x08) { /* USB FIFO full */
  2033. gspca_dev->last_packet_type = DISCARD_PACKET;
  2034. new_qual = -5;
  2035. } else {
  2036. /* else, compute the filling rate and a new JPEG quality */
  2037. r = (sd->pktsz * 100) /
  2038. (sd->npkt *
  2039. gspca_dev->urb[0]->iso_frame_desc[0].length);
  2040. if (r >= 85)
  2041. new_qual = -3;
  2042. else if (r < 75)
  2043. new_qual = 2;
  2044. }
  2045. if (new_qual != 0) {
  2046. sd->nchg += new_qual;
  2047. if (sd->nchg < -6 || sd->nchg >= 12) {
  2048. /* Note: we are in interrupt context, so we can't
  2049. use v4l2_ctrl_g/s_ctrl here. Access the value
  2050. directly instead. */
  2051. s32 curqual = sd->jpegqual->cur.val;
  2052. sd->nchg = 0;
  2053. new_qual += curqual;
  2054. if (new_qual < sd->jpegqual->minimum)
  2055. new_qual = sd->jpegqual->minimum;
  2056. else if (new_qual > sd->jpegqual->maximum)
  2057. new_qual = sd->jpegqual->maximum;
  2058. if (new_qual != curqual) {
  2059. sd->jpegqual->cur.val = new_qual;
  2060. queue_work(sd->work_thread, &sd->work);
  2061. }
  2062. }
  2063. } else {
  2064. sd->nchg = 0;
  2065. }
  2066. sd->pktsz = sd->npkt = 0;
  2067. }
  2068. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  2069. u8 *data, /* isoc packet */
  2070. int len) /* iso packet length */
  2071. {
  2072. struct sd *sd = (struct sd *) gspca_dev;
  2073. int avg_lum, is_jpeg;
  2074. static const u8 frame_header[] =
  2075. {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96};
  2076. is_jpeg = (sd->fmt & 0x03) == 0;
  2077. if (len >= 64 && memcmp(data, frame_header, 6) == 0) {
  2078. avg_lum = ((data[35] >> 2) & 3) |
  2079. (data[20] << 2) |
  2080. (data[19] << 10);
  2081. avg_lum += ((data[35] >> 4) & 3) |
  2082. (data[22] << 2) |
  2083. (data[21] << 10);
  2084. avg_lum += ((data[35] >> 6) & 3) |
  2085. (data[24] << 2) |
  2086. (data[23] << 10);
  2087. avg_lum += (data[36] & 3) |
  2088. (data[26] << 2) |
  2089. (data[25] << 10);
  2090. avg_lum += ((data[36] >> 2) & 3) |
  2091. (data[28] << 2) |
  2092. (data[27] << 10);
  2093. avg_lum += ((data[36] >> 4) & 3) |
  2094. (data[30] << 2) |
  2095. (data[29] << 10);
  2096. avg_lum += ((data[36] >> 6) & 3) |
  2097. (data[32] << 2) |
  2098. (data[31] << 10);
  2099. avg_lum += ((data[44] >> 4) & 3) |
  2100. (data[34] << 2) |
  2101. (data[33] << 10);
  2102. avg_lum >>= 9;
  2103. atomic_set(&sd->avg_lum, avg_lum);
  2104. if (is_jpeg)
  2105. transfer_check(gspca_dev, data);
  2106. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  2107. len -= 64;
  2108. if (len == 0)
  2109. return;
  2110. data += 64;
  2111. }
  2112. if (gspca_dev->last_packet_type == LAST_PACKET) {
  2113. if (is_jpeg) {
  2114. gspca_frame_add(gspca_dev, FIRST_PACKET,
  2115. sd->jpeg_hdr, JPEG_HDR_SZ);
  2116. gspca_frame_add(gspca_dev, INTER_PACKET,
  2117. data, len);
  2118. } else {
  2119. gspca_frame_add(gspca_dev, FIRST_PACKET,
  2120. data, len);
  2121. }
  2122. } else {
  2123. /* if JPEG, count the packets and their size */
  2124. if (is_jpeg) {
  2125. sd->npkt++;
  2126. sd->pktsz += len;
  2127. }
  2128. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  2129. }
  2130. }
  2131. /* sub-driver description */
  2132. static const struct sd_desc sd_desc = {
  2133. .name = KBUILD_MODNAME,
  2134. .config = sd_config,
  2135. .init = sd_init,
  2136. .init_controls = sd_init_controls,
  2137. .isoc_init = sd_isoc_init,
  2138. .start = sd_start,
  2139. .stopN = sd_stopN,
  2140. .stop0 = sd_stop0,
  2141. .pkt_scan = sd_pkt_scan,
  2142. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  2143. .int_pkt_scan = sd_int_pkt_scan,
  2144. #endif
  2145. .dq_callback = sd_dqcallback,
  2146. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2147. .set_register = sd_dbg_s_register,
  2148. .get_register = sd_dbg_g_register,
  2149. #endif
  2150. .get_chip_ident = sd_chip_ident,
  2151. };
  2152. #define SN9C20X(sensor, i2c_addr, flags) \
  2153. .driver_info = ((flags & 0xff) << 16) \
  2154. | (SENSOR_ ## sensor << 8) \
  2155. | (i2c_addr)
  2156. static const struct usb_device_id device_table[] = {
  2157. {USB_DEVICE(0x0c45, 0x6240), SN9C20X(MT9M001, 0x5d, 0)},
  2158. {USB_DEVICE(0x0c45, 0x6242), SN9C20X(MT9M111, 0x5d, 0)},
  2159. {USB_DEVICE(0x0c45, 0x6248), SN9C20X(OV9655, 0x30, 0)},
  2160. {USB_DEVICE(0x0c45, 0x624c), SN9C20X(MT9M112, 0x5d, 0)},
  2161. {USB_DEVICE(0x0c45, 0x624e), SN9C20X(SOI968, 0x30, LED_REVERSE)},
  2162. {USB_DEVICE(0x0c45, 0x624f), SN9C20X(OV9650, 0x30,
  2163. (FLIP_DETECT | HAS_NO_BUTTON))},
  2164. {USB_DEVICE(0x0c45, 0x6251), SN9C20X(OV9650, 0x30, 0)},
  2165. {USB_DEVICE(0x0c45, 0x6253), SN9C20X(OV9650, 0x30, 0)},
  2166. {USB_DEVICE(0x0c45, 0x6260), SN9C20X(OV7670, 0x21, 0)},
  2167. {USB_DEVICE(0x0c45, 0x6270), SN9C20X(MT9VPRB, 0x00, 0)},
  2168. {USB_DEVICE(0x0c45, 0x627b), SN9C20X(OV7660, 0x21, FLIP_DETECT)},
  2169. {USB_DEVICE(0x0c45, 0x627c), SN9C20X(HV7131R, 0x11, 0)},
  2170. {USB_DEVICE(0x0c45, 0x627f), SN9C20X(OV9650, 0x30, 0)},
  2171. {USB_DEVICE(0x0c45, 0x6280), SN9C20X(MT9M001, 0x5d, 0)},
  2172. {USB_DEVICE(0x0c45, 0x6282), SN9C20X(MT9M111, 0x5d, 0)},
  2173. {USB_DEVICE(0x0c45, 0x6288), SN9C20X(OV9655, 0x30, 0)},
  2174. {USB_DEVICE(0x0c45, 0x628c), SN9C20X(MT9M112, 0x5d, 0)},
  2175. {USB_DEVICE(0x0c45, 0x628e), SN9C20X(SOI968, 0x30, 0)},
  2176. {USB_DEVICE(0x0c45, 0x628f), SN9C20X(OV9650, 0x30, 0)},
  2177. {USB_DEVICE(0x0c45, 0x62a0), SN9C20X(OV7670, 0x21, 0)},
  2178. {USB_DEVICE(0x0c45, 0x62b0), SN9C20X(MT9VPRB, 0x00, 0)},
  2179. {USB_DEVICE(0x0c45, 0x62b3), SN9C20X(OV9655, 0x30, LED_REVERSE)},
  2180. {USB_DEVICE(0x0c45, 0x62bb), SN9C20X(OV7660, 0x21, LED_REVERSE)},
  2181. {USB_DEVICE(0x0c45, 0x62bc), SN9C20X(HV7131R, 0x11, 0)},
  2182. {USB_DEVICE(0x045e, 0x00f4), SN9C20X(OV9650, 0x30, 0)},
  2183. {USB_DEVICE(0x145f, 0x013d), SN9C20X(OV7660, 0x21, 0)},
  2184. {USB_DEVICE(0x0458, 0x7029), SN9C20X(HV7131R, 0x11, 0)},
  2185. {USB_DEVICE(0x0458, 0x704a), SN9C20X(MT9M112, 0x5d, 0)},
  2186. {USB_DEVICE(0x0458, 0x704c), SN9C20X(MT9M112, 0x5d, 0)},
  2187. {USB_DEVICE(0xa168, 0x0610), SN9C20X(HV7131R, 0x11, 0)},
  2188. {USB_DEVICE(0xa168, 0x0611), SN9C20X(HV7131R, 0x11, 0)},
  2189. {USB_DEVICE(0xa168, 0x0613), SN9C20X(HV7131R, 0x11, 0)},
  2190. {USB_DEVICE(0xa168, 0x0618), SN9C20X(HV7131R, 0x11, 0)},
  2191. {USB_DEVICE(0xa168, 0x0614), SN9C20X(MT9M111, 0x5d, 0)},
  2192. {USB_DEVICE(0xa168, 0x0615), SN9C20X(MT9M111, 0x5d, 0)},
  2193. {USB_DEVICE(0xa168, 0x0617), SN9C20X(MT9M111, 0x5d, 0)},
  2194. {}
  2195. };
  2196. MODULE_DEVICE_TABLE(usb, device_table);
  2197. /* -- device connect -- */
  2198. static int sd_probe(struct usb_interface *intf,
  2199. const struct usb_device_id *id)
  2200. {
  2201. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  2202. THIS_MODULE);
  2203. }
  2204. static struct usb_driver sd_driver = {
  2205. .name = KBUILD_MODNAME,
  2206. .id_table = device_table,
  2207. .probe = sd_probe,
  2208. .disconnect = gspca_disconnect,
  2209. #ifdef CONFIG_PM
  2210. .suspend = gspca_suspend,
  2211. .resume = gspca_resume,
  2212. .reset_resume = gspca_resume,
  2213. #endif
  2214. };
  2215. module_usb_driver(sd_driver);