sn9c20x.c 70 KB

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