sn9c20x.c 70 KB

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