sn9c20x.c 71 KB

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