sn9c20x.c 70 KB

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