sn9c20x.c 69 KB

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