sn9c20x.c 70 KB

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