sn9c20x.c 69 KB

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