sq930x.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. /*
  2. * SQ930x subdriver
  3. *
  4. * Copyright (C) 2010 Jean-François Moine <http://moinejf.free.fr>
  5. * Copyright (C) 2006 -2008 Gerard Klaver <gerard at gkall dot hobby dot nl>
  6. * Copyright (C) 2007 Sam Revitch <samr7@cs.washington.edu>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #define MODULE_NAME "sq930x"
  20. #include "gspca.h"
  21. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>\n"
  22. "Gerard Klaver <gerard at gkall dot hobby dot nl\n"
  23. "Sam Revitch <samr7@cs.washington.edu>");
  24. MODULE_DESCRIPTION("GSPCA/SQ930x USB Camera Driver");
  25. MODULE_LICENSE("GPL");
  26. /* Structure to hold all of our device specific stuff */
  27. struct sd {
  28. struct gspca_dev gspca_dev; /* !! must be the first item */
  29. struct { /* exposure/gain control cluster */
  30. struct v4l2_ctrl *exposure;
  31. struct v4l2_ctrl *gain;
  32. };
  33. u8 do_ctrl;
  34. u8 gpio[2];
  35. u8 sensor;
  36. u8 type;
  37. #define Generic 0
  38. #define Creative_live_motion 1
  39. };
  40. enum sensors {
  41. SENSOR_ICX098BQ,
  42. SENSOR_LZ24BP,
  43. SENSOR_MI0360,
  44. SENSOR_MT9V111, /* = MI360SOC */
  45. SENSOR_OV7660,
  46. SENSOR_OV9630,
  47. };
  48. static struct v4l2_pix_format vga_mode[] = {
  49. {320, 240, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
  50. .bytesperline = 320,
  51. .sizeimage = 320 * 240,
  52. .colorspace = V4L2_COLORSPACE_SRGB,
  53. .priv = 0},
  54. {640, 480, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
  55. .bytesperline = 640,
  56. .sizeimage = 640 * 480,
  57. .colorspace = V4L2_COLORSPACE_SRGB,
  58. .priv = 1},
  59. };
  60. /* sq930x registers */
  61. #define SQ930_CTRL_UCBUS_IO 0x0001
  62. #define SQ930_CTRL_I2C_IO 0x0002
  63. #define SQ930_CTRL_GPIO 0x0005
  64. #define SQ930_CTRL_CAP_START 0x0010
  65. #define SQ930_CTRL_CAP_STOP 0x0011
  66. #define SQ930_CTRL_SET_EXPOSURE 0x001d
  67. #define SQ930_CTRL_RESET 0x001e
  68. #define SQ930_CTRL_GET_DEV_INFO 0x001f
  69. /* gpio 1 (8..15) */
  70. #define SQ930_GPIO_DFL_I2C_SDA 0x0001
  71. #define SQ930_GPIO_DFL_I2C_SCL 0x0002
  72. #define SQ930_GPIO_RSTBAR 0x0004
  73. #define SQ930_GPIO_EXTRA1 0x0040
  74. #define SQ930_GPIO_EXTRA2 0x0080
  75. /* gpio 3 (24..31) */
  76. #define SQ930_GPIO_POWER 0x0200
  77. #define SQ930_GPIO_DFL_LED 0x1000
  78. struct ucbus_write_cmd {
  79. u16 bw_addr;
  80. u8 bw_data;
  81. };
  82. struct i2c_write_cmd {
  83. u8 reg;
  84. u16 val;
  85. };
  86. static const struct ucbus_write_cmd icx098bq_start_0[] = {
  87. {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xce},
  88. {0xf802, 0xc1}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x0e},
  89. {0xf80a, 0x01}, {0xf80b, 0xee}, {0xf807, 0x60}, {0xf80c, 0x02},
  90. {0xf80d, 0xf0}, {0xf80e, 0x03}, {0xf80f, 0x0a}, {0xf81c, 0x02},
  91. {0xf81d, 0xf0}, {0xf81e, 0x03}, {0xf81f, 0x0a}, {0xf83a, 0x00},
  92. {0xf83b, 0x10}, {0xf83c, 0x00}, {0xf83d, 0x4e}, {0xf810, 0x04},
  93. {0xf811, 0x00}, {0xf812, 0x02}, {0xf813, 0x10}, {0xf803, 0x00},
  94. {0xf814, 0x01}, {0xf815, 0x18}, {0xf816, 0x00}, {0xf817, 0x48},
  95. {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
  96. {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
  97. {0xf823, 0x07}, {0xf824, 0xff}, {0xf825, 0x03}, {0xf826, 0xff},
  98. {0xf827, 0x06}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
  99. {0xf82b, 0x0c}, {0xf82c, 0xfd}, {0xf82d, 0x01}, {0xf82e, 0x00},
  100. {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
  101. {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
  102. {0xf854, 0x00}, {0xf855, 0x18}, {0xf856, 0x00}, {0xf857, 0x3c},
  103. {0xf858, 0x00}, {0xf859, 0x0c}, {0xf85a, 0x00}, {0xf85b, 0x30},
  104. {0xf85c, 0x00}, {0xf85d, 0x0c}, {0xf85e, 0x00}, {0xf85f, 0x30},
  105. {0xf860, 0x00}, {0xf861, 0x48}, {0xf862, 0x01}, {0xf863, 0xdc},
  106. {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
  107. {0xf868, 0xff}, {0xf869, 0x70}, {0xf86c, 0xff}, {0xf86d, 0x00},
  108. {0xf86a, 0xff}, {0xf86b, 0x48}, {0xf86e, 0xff}, {0xf86f, 0x00},
  109. {0xf870, 0x01}, {0xf871, 0xdb}, {0xf872, 0x01}, {0xf873, 0xfa},
  110. {0xf874, 0x01}, {0xf875, 0xdb}, {0xf876, 0x01}, {0xf877, 0xfa},
  111. {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
  112. {0xf800, 0x03}
  113. };
  114. static const struct ucbus_write_cmd icx098bq_start_1[] = {
  115. {0xf5f0, 0x00}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  116. {0xf5f4, 0xc0},
  117. {0xf5f0, 0x49}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  118. {0xf5f4, 0xc0},
  119. {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
  120. {0xf5f9, 0x00}
  121. };
  122. static const struct ucbus_write_cmd icx098bq_start_2[] = {
  123. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x82}, {0xf806, 0x00},
  124. {0xf807, 0x7f}, {0xf800, 0x03},
  125. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x40}, {0xf806, 0x00},
  126. {0xf807, 0x7f}, {0xf800, 0x03},
  127. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xcf}, {0xf806, 0xd0},
  128. {0xf807, 0x7f}, {0xf800, 0x03},
  129. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
  130. {0xf807, 0x7f}, {0xf800, 0x03}
  131. };
  132. static const struct ucbus_write_cmd lz24bp_start_0[] = {
  133. {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xbe},
  134. {0xf802, 0xc6}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x06},
  135. {0xf80a, 0x01}, {0xf80b, 0xfe}, {0xf807, 0x84}, {0xf80c, 0x02},
  136. {0xf80d, 0xf7}, {0xf80e, 0x03}, {0xf80f, 0x0b}, {0xf81c, 0x00},
  137. {0xf81d, 0x49}, {0xf81e, 0x03}, {0xf81f, 0x0b}, {0xf83a, 0x00},
  138. {0xf83b, 0x01}, {0xf83c, 0x00}, {0xf83d, 0x6b}, {0xf810, 0x03},
  139. {0xf811, 0x10}, {0xf812, 0x02}, {0xf813, 0x6f}, {0xf803, 0x00},
  140. {0xf814, 0x00}, {0xf815, 0x44}, {0xf816, 0x00}, {0xf817, 0x48},
  141. {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
  142. {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
  143. {0xf823, 0x07}, {0xf824, 0xfd}, {0xf825, 0x07}, {0xf826, 0xf0},
  144. {0xf827, 0x0c}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
  145. {0xf82b, 0x0c}, {0xf82c, 0xfc}, {0xf82d, 0x01}, {0xf82e, 0x00},
  146. {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
  147. {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
  148. {0xf854, 0x00}, {0xf855, 0x0c}, {0xf856, 0x00}, {0xf857, 0x30},
  149. {0xf858, 0x00}, {0xf859, 0x18}, {0xf85a, 0x00}, {0xf85b, 0x3c},
  150. {0xf85c, 0x00}, {0xf85d, 0x18}, {0xf85e, 0x00}, {0xf85f, 0x3c},
  151. {0xf860, 0xff}, {0xf861, 0x37}, {0xf862, 0xff}, {0xf863, 0x1d},
  152. {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
  153. {0xf868, 0x00}, {0xf869, 0x37}, {0xf86c, 0x02}, {0xf86d, 0x1d},
  154. {0xf86a, 0x00}, {0xf86b, 0x37}, {0xf86e, 0x02}, {0xf86f, 0x1d},
  155. {0xf870, 0x01}, {0xf871, 0xc6}, {0xf872, 0x02}, {0xf873, 0x04},
  156. {0xf874, 0x01}, {0xf875, 0xc6}, {0xf876, 0x02}, {0xf877, 0x04},
  157. {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
  158. {0xf800, 0x03}
  159. };
  160. static const struct ucbus_write_cmd lz24bp_start_1_gen[] = {
  161. {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  162. {0xf5f4, 0xb3},
  163. {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  164. {0xf5f4, 0xb3},
  165. {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
  166. {0xf5f9, 0x00}
  167. };
  168. static const struct ucbus_write_cmd lz24bp_start_1_clm[] = {
  169. {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
  170. {0xf5f4, 0xc0},
  171. {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
  172. {0xf5f4, 0xc0},
  173. {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
  174. {0xf5f9, 0x00}
  175. };
  176. static const struct ucbus_write_cmd lz24bp_start_2[] = {
  177. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x80}, {0xf806, 0x00},
  178. {0xf807, 0x7f}, {0xf800, 0x03},
  179. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x4e}, {0xf806, 0x00},
  180. {0xf807, 0x7f}, {0xf800, 0x03},
  181. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xc0}, {0xf806, 0x48},
  182. {0xf807, 0x7f}, {0xf800, 0x03},
  183. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
  184. {0xf807, 0x7f}, {0xf800, 0x03}
  185. };
  186. static const struct ucbus_write_cmd mi0360_start_0[] = {
  187. {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0xcc}, {0xf333, 0xcc},
  188. {0xf334, 0xcc}, {0xf335, 0xcc}, {0xf33f, 0x00}
  189. };
  190. static const struct i2c_write_cmd mi0360_init_23[] = {
  191. {0x30, 0x0040}, /* reserved - def 0x0005 */
  192. {0x31, 0x0000}, /* reserved - def 0x002a */
  193. {0x34, 0x0100}, /* reserved - def 0x0100 */
  194. {0x3d, 0x068f}, /* reserved - def 0x068f */
  195. };
  196. static const struct i2c_write_cmd mi0360_init_24[] = {
  197. {0x03, 0x01e5}, /* window height */
  198. {0x04, 0x0285}, /* window width */
  199. };
  200. static const struct i2c_write_cmd mi0360_init_25[] = {
  201. {0x35, 0x0020}, /* global gain */
  202. {0x2b, 0x0020}, /* green1 gain */
  203. {0x2c, 0x002a}, /* blue gain */
  204. {0x2d, 0x0028}, /* red gain */
  205. {0x2e, 0x0020}, /* green2 gain */
  206. };
  207. static const struct ucbus_write_cmd mi0360_start_1[] = {
  208. {0xf5f0, 0x11}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  209. {0xf5f4, 0xa6},
  210. {0xf5f0, 0x51}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  211. {0xf5f4, 0xa6},
  212. {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
  213. {0xf5f9, 0x00}
  214. };
  215. static const struct i2c_write_cmd mi0360_start_2[] = {
  216. {0x62, 0x041d}, /* reserved - def 0x0418 */
  217. };
  218. static const struct i2c_write_cmd mi0360_start_3[] = {
  219. {0x05, 0x007b}, /* horiz blanking */
  220. };
  221. static const struct i2c_write_cmd mi0360_start_4[] = {
  222. {0x05, 0x03f5}, /* horiz blanking */
  223. };
  224. static const struct i2c_write_cmd mt9v111_init_0[] = {
  225. {0x01, 0x0001}, /* select IFP/SOC registers */
  226. {0x06, 0x300c}, /* operating mode control */
  227. {0x08, 0xcc00}, /* output format control (RGB) */
  228. {0x01, 0x0004}, /* select sensor core registers */
  229. };
  230. static const struct i2c_write_cmd mt9v111_init_1[] = {
  231. {0x03, 0x01e5}, /* window height */
  232. {0x04, 0x0285}, /* window width */
  233. };
  234. static const struct i2c_write_cmd mt9v111_init_2[] = {
  235. {0x30, 0x7800},
  236. {0x31, 0x0000},
  237. {0x07, 0x3002}, /* output control */
  238. {0x35, 0x0020}, /* global gain */
  239. {0x2b, 0x0020}, /* green1 gain */
  240. {0x2c, 0x0020}, /* blue gain */
  241. {0x2d, 0x0020}, /* red gain */
  242. {0x2e, 0x0020}, /* green2 gain */
  243. };
  244. static const struct ucbus_write_cmd mt9v111_start_1[] = {
  245. {0xf5f0, 0x11}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  246. {0xf5f4, 0xaa},
  247. {0xf5f0, 0x51}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  248. {0xf5f4, 0xaa},
  249. {0xf5fa, 0x00}, {0xf5f6, 0x0a}, {0xf5f7, 0x0a}, {0xf5f8, 0x0a},
  250. {0xf5f9, 0x0a}
  251. };
  252. static const struct i2c_write_cmd mt9v111_init_3[] = {
  253. {0x62, 0x0405},
  254. };
  255. static const struct i2c_write_cmd mt9v111_init_4[] = {
  256. /* {0x05, 0x00ce}, */
  257. {0x05, 0x005d}, /* horizontal blanking */
  258. };
  259. static const struct ucbus_write_cmd ov7660_start_0[] = {
  260. {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0xc0},
  261. {0xf334, 0x39}, {0xf335, 0xe7}, {0xf33f, 0x03}
  262. };
  263. static const struct ucbus_write_cmd ov9630_start_0[] = {
  264. {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0x00},
  265. {0xf334, 0x3e}, {0xf335, 0xf8}, {0xf33f, 0x03}
  266. };
  267. /* start parameters indexed by [sensor][mode] */
  268. static const struct cap_s {
  269. u8 cc_sizeid;
  270. u8 cc_bytes[32];
  271. } capconfig[4][2] = {
  272. [SENSOR_ICX098BQ] = {
  273. {2, /* Bayer 320x240 */
  274. {0x05, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
  275. 0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  276. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0,
  277. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
  278. {4, /* Bayer 640x480 */
  279. {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
  280. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  281. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  282. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
  283. },
  284. [SENSOR_LZ24BP] = {
  285. {2, /* Bayer 320x240 */
  286. {0x05, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
  287. 0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  288. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  289. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
  290. {4, /* Bayer 640x480 */
  291. {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
  292. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  293. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  294. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
  295. },
  296. [SENSOR_MI0360] = {
  297. {2, /* Bayer 320x240 */
  298. {0x05, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
  299. 0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  300. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  301. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
  302. {4, /* Bayer 640x480 */
  303. {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
  304. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  305. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  306. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
  307. },
  308. [SENSOR_MT9V111] = {
  309. {2, /* Bayer 320x240 */
  310. {0x05, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
  311. 0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  312. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  313. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
  314. {4, /* Bayer 640x480 */
  315. {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
  316. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  317. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  318. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
  319. },
  320. };
  321. struct sensor_s {
  322. const char *name;
  323. u8 i2c_addr;
  324. u8 i2c_dum;
  325. u8 gpio[5];
  326. u8 cmd_len;
  327. const struct ucbus_write_cmd *cmd;
  328. };
  329. static const struct sensor_s sensor_tb[] = {
  330. [SENSOR_ICX098BQ] = {
  331. "icx098bp",
  332. 0x00, 0x00,
  333. {0,
  334. SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
  335. SQ930_GPIO_DFL_I2C_SDA,
  336. 0,
  337. SQ930_GPIO_RSTBAR
  338. },
  339. 8, icx098bq_start_0
  340. },
  341. [SENSOR_LZ24BP] = {
  342. "lz24bp",
  343. 0x00, 0x00,
  344. {0,
  345. SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
  346. SQ930_GPIO_DFL_I2C_SDA,
  347. 0,
  348. SQ930_GPIO_RSTBAR
  349. },
  350. 8, lz24bp_start_0
  351. },
  352. [SENSOR_MI0360] = {
  353. "mi0360",
  354. 0x5d, 0x80,
  355. {SQ930_GPIO_RSTBAR,
  356. SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
  357. SQ930_GPIO_DFL_I2C_SDA,
  358. 0,
  359. 0
  360. },
  361. 7, mi0360_start_0
  362. },
  363. [SENSOR_MT9V111] = {
  364. "mt9v111",
  365. 0x5c, 0x7f,
  366. {SQ930_GPIO_RSTBAR,
  367. SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
  368. SQ930_GPIO_DFL_I2C_SDA,
  369. 0,
  370. 0
  371. },
  372. 7, mi0360_start_0
  373. },
  374. [SENSOR_OV7660] = {
  375. "ov7660",
  376. 0x21, 0x00,
  377. {0,
  378. SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
  379. SQ930_GPIO_DFL_I2C_SDA,
  380. 0,
  381. SQ930_GPIO_RSTBAR
  382. },
  383. 7, ov7660_start_0
  384. },
  385. [SENSOR_OV9630] = {
  386. "ov9630",
  387. 0x30, 0x00,
  388. {0,
  389. SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
  390. SQ930_GPIO_DFL_I2C_SDA,
  391. 0,
  392. SQ930_GPIO_RSTBAR
  393. },
  394. 7, ov9630_start_0
  395. },
  396. };
  397. static void reg_r(struct gspca_dev *gspca_dev,
  398. u16 value, int len)
  399. {
  400. int ret;
  401. if (gspca_dev->usb_err < 0)
  402. return;
  403. ret = usb_control_msg(gspca_dev->dev,
  404. usb_rcvctrlpipe(gspca_dev->dev, 0),
  405. 0x0c,
  406. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  407. value, 0, gspca_dev->usb_buf, len,
  408. 500);
  409. if (ret < 0) {
  410. pr_err("reg_r %04x failed %d\n", value, ret);
  411. gspca_dev->usb_err = ret;
  412. /*
  413. * Make sure the buffer is zeroed to avoid uninitialized
  414. * values.
  415. */
  416. memset(gspca_dev->usb_buf, 0, USB_BUF_SZ);
  417. }
  418. }
  419. static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
  420. {
  421. int ret;
  422. if (gspca_dev->usb_err < 0)
  423. return;
  424. gspca_dbg(gspca_dev, D_USBO, "reg_w v: %04x i: %04x\n", value, index);
  425. ret = usb_control_msg(gspca_dev->dev,
  426. usb_sndctrlpipe(gspca_dev->dev, 0),
  427. 0x0c, /* request */
  428. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  429. value, index, NULL, 0,
  430. 500);
  431. msleep(30);
  432. if (ret < 0) {
  433. pr_err("reg_w %04x %04x failed %d\n", value, index, ret);
  434. gspca_dev->usb_err = ret;
  435. }
  436. }
  437. static void reg_wb(struct gspca_dev *gspca_dev, u16 value, u16 index,
  438. const u8 *data, int len)
  439. {
  440. int ret;
  441. if (gspca_dev->usb_err < 0)
  442. return;
  443. gspca_dbg(gspca_dev, D_USBO, "reg_wb v: %04x i: %04x %02x...%02x\n",
  444. value, index, *data, data[len - 1]);
  445. memcpy(gspca_dev->usb_buf, data, len);
  446. ret = usb_control_msg(gspca_dev->dev,
  447. usb_sndctrlpipe(gspca_dev->dev, 0),
  448. 0x0c, /* request */
  449. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  450. value, index, gspca_dev->usb_buf, len,
  451. 1000);
  452. msleep(30);
  453. if (ret < 0) {
  454. pr_err("reg_wb %04x %04x failed %d\n", value, index, ret);
  455. gspca_dev->usb_err = ret;
  456. }
  457. }
  458. static void i2c_write(struct sd *sd,
  459. const struct i2c_write_cmd *cmd,
  460. int ncmds)
  461. {
  462. struct gspca_dev *gspca_dev = &sd->gspca_dev;
  463. const struct sensor_s *sensor;
  464. u16 val, idx;
  465. u8 *buf;
  466. int ret;
  467. if (gspca_dev->usb_err < 0)
  468. return;
  469. sensor = &sensor_tb[sd->sensor];
  470. val = (sensor->i2c_addr << 8) | SQ930_CTRL_I2C_IO;
  471. idx = (cmd->val & 0xff00) | cmd->reg;
  472. buf = gspca_dev->usb_buf;
  473. *buf++ = sensor->i2c_dum;
  474. *buf++ = cmd->val;
  475. while (--ncmds > 0) {
  476. cmd++;
  477. *buf++ = cmd->reg;
  478. *buf++ = cmd->val >> 8;
  479. *buf++ = sensor->i2c_dum;
  480. *buf++ = cmd->val;
  481. }
  482. gspca_dbg(gspca_dev, D_USBO, "i2c_w v: %04x i: %04x %02x...%02x\n",
  483. val, idx, gspca_dev->usb_buf[0], buf[-1]);
  484. ret = usb_control_msg(gspca_dev->dev,
  485. usb_sndctrlpipe(gspca_dev->dev, 0),
  486. 0x0c, /* request */
  487. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  488. val, idx,
  489. gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
  490. 500);
  491. if (ret < 0) {
  492. pr_err("i2c_write failed %d\n", ret);
  493. gspca_dev->usb_err = ret;
  494. }
  495. }
  496. static void ucbus_write(struct gspca_dev *gspca_dev,
  497. const struct ucbus_write_cmd *cmd,
  498. int ncmds,
  499. int batchsize)
  500. {
  501. u8 *buf;
  502. u16 val, idx;
  503. int len, ret;
  504. if (gspca_dev->usb_err < 0)
  505. return;
  506. if ((batchsize - 1) * 3 > USB_BUF_SZ) {
  507. gspca_err(gspca_dev, "Bug: usb_buf overflow\n");
  508. gspca_dev->usb_err = -ENOMEM;
  509. return;
  510. }
  511. for (;;) {
  512. len = ncmds;
  513. if (len > batchsize)
  514. len = batchsize;
  515. ncmds -= len;
  516. val = (cmd->bw_addr << 8) | SQ930_CTRL_UCBUS_IO;
  517. idx = (cmd->bw_data << 8) | (cmd->bw_addr >> 8);
  518. buf = gspca_dev->usb_buf;
  519. while (--len > 0) {
  520. cmd++;
  521. *buf++ = cmd->bw_addr;
  522. *buf++ = cmd->bw_addr >> 8;
  523. *buf++ = cmd->bw_data;
  524. }
  525. if (buf != gspca_dev->usb_buf)
  526. gspca_dbg(gspca_dev, D_USBO, "ucbus v: %04x i: %04x %02x...%02x\n",
  527. val, idx,
  528. gspca_dev->usb_buf[0], buf[-1]);
  529. else
  530. gspca_dbg(gspca_dev, D_USBO, "ucbus v: %04x i: %04x\n",
  531. val, idx);
  532. ret = usb_control_msg(gspca_dev->dev,
  533. usb_sndctrlpipe(gspca_dev->dev, 0),
  534. 0x0c, /* request */
  535. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  536. val, idx,
  537. gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
  538. 500);
  539. if (ret < 0) {
  540. pr_err("ucbus_write failed %d\n", ret);
  541. gspca_dev->usb_err = ret;
  542. return;
  543. }
  544. msleep(30);
  545. if (ncmds <= 0)
  546. break;
  547. cmd++;
  548. }
  549. }
  550. static void gpio_set(struct sd *sd, u16 val, u16 mask)
  551. {
  552. struct gspca_dev *gspca_dev = &sd->gspca_dev;
  553. if (mask & 0x00ff) {
  554. sd->gpio[0] &= ~mask;
  555. sd->gpio[0] |= val;
  556. reg_w(gspca_dev, 0x0100 | SQ930_CTRL_GPIO,
  557. ~sd->gpio[0] << 8);
  558. }
  559. mask >>= 8;
  560. val >>= 8;
  561. if (mask) {
  562. sd->gpio[1] &= ~mask;
  563. sd->gpio[1] |= val;
  564. reg_w(gspca_dev, 0x0300 | SQ930_CTRL_GPIO,
  565. ~sd->gpio[1] << 8);
  566. }
  567. }
  568. static void gpio_init(struct sd *sd,
  569. const u8 *gpio)
  570. {
  571. gpio_set(sd, *gpio++, 0x000f);
  572. gpio_set(sd, *gpio++, 0x000f);
  573. gpio_set(sd, *gpio++, 0x000f);
  574. gpio_set(sd, *gpio++, 0x000f);
  575. gpio_set(sd, *gpio, 0x000f);
  576. }
  577. static void bridge_init(struct sd *sd)
  578. {
  579. static const struct ucbus_write_cmd clkfreq_cmd = {
  580. 0xf031, 0 /* SQ930_CLKFREQ_60MHZ */
  581. };
  582. ucbus_write(&sd->gspca_dev, &clkfreq_cmd, 1, 1);
  583. gpio_set(sd, SQ930_GPIO_POWER, 0xff00);
  584. }
  585. static void cmos_probe(struct gspca_dev *gspca_dev)
  586. {
  587. struct sd *sd = (struct sd *) gspca_dev;
  588. int i;
  589. const struct sensor_s *sensor;
  590. static const u8 probe_order[] = {
  591. /* SENSOR_LZ24BP, (tested as ccd) */
  592. SENSOR_OV9630,
  593. SENSOR_MI0360,
  594. SENSOR_OV7660,
  595. SENSOR_MT9V111,
  596. };
  597. for (i = 0; i < ARRAY_SIZE(probe_order); i++) {
  598. sensor = &sensor_tb[probe_order[i]];
  599. ucbus_write(&sd->gspca_dev, sensor->cmd, sensor->cmd_len, 8);
  600. gpio_init(sd, sensor->gpio);
  601. msleep(100);
  602. reg_r(gspca_dev, (sensor->i2c_addr << 8) | 0x001c, 1);
  603. msleep(100);
  604. if (gspca_dev->usb_buf[0] != 0)
  605. break;
  606. }
  607. if (i >= ARRAY_SIZE(probe_order)) {
  608. pr_err("Unknown sensor\n");
  609. gspca_dev->usb_err = -EINVAL;
  610. return;
  611. }
  612. sd->sensor = probe_order[i];
  613. switch (sd->sensor) {
  614. case SENSOR_OV7660:
  615. case SENSOR_OV9630:
  616. pr_err("Sensor %s not yet treated\n",
  617. sensor_tb[sd->sensor].name);
  618. gspca_dev->usb_err = -EINVAL;
  619. break;
  620. }
  621. }
  622. static void mt9v111_init(struct gspca_dev *gspca_dev)
  623. {
  624. int i, nwait;
  625. static const u8 cmd_001b[] = {
  626. 0x00, 0x3b, 0xf6, 0x01, 0x03, 0x02, 0x00, 0x00,
  627. 0x00, 0x00, 0x00
  628. };
  629. static const u8 cmd_011b[][7] = {
  630. {0x10, 0x01, 0x66, 0x08, 0x00, 0x00, 0x00},
  631. {0x01, 0x00, 0x1a, 0x04, 0x00, 0x00, 0x00},
  632. {0x20, 0x00, 0x10, 0x04, 0x00, 0x00, 0x00},
  633. {0x02, 0x01, 0xae, 0x01, 0x00, 0x00, 0x00},
  634. };
  635. reg_wb(gspca_dev, 0x001b, 0x0000, cmd_001b, sizeof cmd_001b);
  636. for (i = 0; i < ARRAY_SIZE(cmd_011b); i++) {
  637. reg_wb(gspca_dev, 0x001b, 0x0000, cmd_011b[i],
  638. ARRAY_SIZE(cmd_011b[0]));
  639. msleep(400);
  640. nwait = 20;
  641. for (;;) {
  642. reg_r(gspca_dev, 0x031b, 1);
  643. if (gspca_dev->usb_buf[0] == 0
  644. || gspca_dev->usb_err != 0)
  645. break;
  646. if (--nwait < 0) {
  647. gspca_dbg(gspca_dev, D_PROBE, "mt9v111_init timeout\n");
  648. gspca_dev->usb_err = -ETIME;
  649. return;
  650. }
  651. msleep(50);
  652. }
  653. }
  654. }
  655. static void global_init(struct sd *sd, int first_time)
  656. {
  657. switch (sd->sensor) {
  658. case SENSOR_ICX098BQ:
  659. if (first_time)
  660. ucbus_write(&sd->gspca_dev,
  661. icx098bq_start_0,
  662. 8, 8);
  663. gpio_init(sd, sensor_tb[sd->sensor].gpio);
  664. break;
  665. case SENSOR_LZ24BP:
  666. if (sd->type != Creative_live_motion)
  667. gpio_set(sd, SQ930_GPIO_EXTRA1, 0x00ff);
  668. else
  669. gpio_set(sd, 0, 0x00ff);
  670. msleep(50);
  671. if (first_time)
  672. ucbus_write(&sd->gspca_dev,
  673. lz24bp_start_0,
  674. 8, 8);
  675. gpio_init(sd, sensor_tb[sd->sensor].gpio);
  676. break;
  677. case SENSOR_MI0360:
  678. if (first_time)
  679. ucbus_write(&sd->gspca_dev,
  680. mi0360_start_0,
  681. ARRAY_SIZE(mi0360_start_0),
  682. 8);
  683. gpio_init(sd, sensor_tb[sd->sensor].gpio);
  684. gpio_set(sd, SQ930_GPIO_EXTRA2, SQ930_GPIO_EXTRA2);
  685. break;
  686. default:
  687. /* case SENSOR_MT9V111: */
  688. if (first_time)
  689. mt9v111_init(&sd->gspca_dev);
  690. else
  691. gpio_init(sd, sensor_tb[sd->sensor].gpio);
  692. break;
  693. }
  694. }
  695. static void lz24bp_ppl(struct sd *sd, u16 ppl)
  696. {
  697. struct ucbus_write_cmd cmds[2] = {
  698. {0xf810, ppl >> 8},
  699. {0xf811, ppl}
  700. };
  701. ucbus_write(&sd->gspca_dev, cmds, ARRAY_SIZE(cmds), 2);
  702. }
  703. static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 gain)
  704. {
  705. struct sd *sd = (struct sd *) gspca_dev;
  706. int i, integclks, intstartclk, frameclks, min_frclk;
  707. const struct sensor_s *sensor;
  708. u16 cmd;
  709. u8 buf[15];
  710. integclks = expo;
  711. i = 0;
  712. cmd = SQ930_CTRL_SET_EXPOSURE;
  713. switch (sd->sensor) {
  714. case SENSOR_ICX098BQ: /* ccd */
  715. case SENSOR_LZ24BP:
  716. min_frclk = sd->sensor == SENSOR_ICX098BQ ? 0x210 : 0x26f;
  717. if (integclks >= min_frclk) {
  718. intstartclk = 0;
  719. frameclks = integclks;
  720. } else {
  721. intstartclk = min_frclk - integclks;
  722. frameclks = min_frclk;
  723. }
  724. buf[i++] = intstartclk >> 8;
  725. buf[i++] = intstartclk;
  726. buf[i++] = frameclks >> 8;
  727. buf[i++] = frameclks;
  728. buf[i++] = gain;
  729. break;
  730. default: /* cmos */
  731. /* case SENSOR_MI0360: */
  732. /* case SENSOR_MT9V111: */
  733. cmd |= 0x0100;
  734. sensor = &sensor_tb[sd->sensor];
  735. buf[i++] = sensor->i2c_addr; /* i2c_slave_addr */
  736. buf[i++] = 0x08; /* 2 * ni2c */
  737. buf[i++] = 0x09; /* reg = shutter width */
  738. buf[i++] = integclks >> 8; /* val H */
  739. buf[i++] = sensor->i2c_dum;
  740. buf[i++] = integclks; /* val L */
  741. buf[i++] = 0x35; /* reg = global gain */
  742. buf[i++] = 0x00; /* val H */
  743. buf[i++] = sensor->i2c_dum;
  744. buf[i++] = 0x80 + gain / 2; /* val L */
  745. buf[i++] = 0x00;
  746. buf[i++] = 0x00;
  747. buf[i++] = 0x00;
  748. buf[i++] = 0x00;
  749. buf[i++] = 0x83;
  750. break;
  751. }
  752. reg_wb(gspca_dev, cmd, 0, buf, i);
  753. }
  754. /* This function is called at probe time just before sd_init */
  755. static int sd_config(struct gspca_dev *gspca_dev,
  756. const struct usb_device_id *id)
  757. {
  758. struct sd *sd = (struct sd *) gspca_dev;
  759. struct cam *cam = &gspca_dev->cam;
  760. sd->sensor = id->driver_info >> 8;
  761. sd->type = id->driver_info;
  762. cam->cam_mode = vga_mode;
  763. cam->nmodes = ARRAY_SIZE(vga_mode);
  764. cam->bulk = 1;
  765. return 0;
  766. }
  767. /* this function is called at probe and resume time */
  768. static int sd_init(struct gspca_dev *gspca_dev)
  769. {
  770. struct sd *sd = (struct sd *) gspca_dev;
  771. sd->gpio[0] = sd->gpio[1] = 0xff; /* force gpio rewrite */
  772. /*fixme: is this needed for icx098bp and mi0360?
  773. if (sd->sensor != SENSOR_LZ24BP)
  774. reg_w(gspca_dev, SQ930_CTRL_RESET, 0x0000);
  775. */
  776. reg_r(gspca_dev, SQ930_CTRL_GET_DEV_INFO, 8);
  777. if (gspca_dev->usb_err < 0)
  778. return gspca_dev->usb_err;
  779. /* it returns:
  780. * 03 00 12 93 0b f6 c9 00 live! ultra
  781. * 03 00 07 93 0b f6 ca 00 live! ultra for notebook
  782. * 03 00 12 93 0b fe c8 00 Trust WB-3500T
  783. * 02 00 06 93 0b fe c8 00 Joy-IT 318S
  784. * 03 00 12 93 0b f6 cf 00 icam tracer - sensor icx098bq
  785. * 02 00 12 93 0b fe cf 00 ProQ Motion Webcam
  786. *
  787. * byte
  788. * 0: 02 = usb 1.0 (12Mbit) / 03 = usb2.0 (480Mbit)
  789. * 1: 00
  790. * 2: 06 / 07 / 12 = mode webcam? firmware??
  791. * 3: 93 chip = 930b (930b or 930c)
  792. * 4: 0b
  793. * 5: f6 = cdd (icx098bq, lz24bp) / fe or de = cmos (i2c) (other sensors)
  794. * 6: c8 / c9 / ca / cf = mode webcam?, sensor? webcam?
  795. * 7: 00
  796. */
  797. gspca_dbg(gspca_dev, D_PROBE, "info: %*ph\n", 8, gspca_dev->usb_buf);
  798. bridge_init(sd);
  799. if (sd->sensor == SENSOR_MI0360) {
  800. /* no sensor probe for icam tracer */
  801. if (gspca_dev->usb_buf[5] == 0xf6) /* if ccd */
  802. sd->sensor = SENSOR_ICX098BQ;
  803. else
  804. cmos_probe(gspca_dev);
  805. }
  806. if (gspca_dev->usb_err >= 0) {
  807. gspca_dbg(gspca_dev, D_PROBE, "Sensor %s\n",
  808. sensor_tb[sd->sensor].name);
  809. global_init(sd, 1);
  810. }
  811. return gspca_dev->usb_err;
  812. }
  813. /* send the start/stop commands to the webcam */
  814. static void send_start(struct gspca_dev *gspca_dev)
  815. {
  816. struct sd *sd = (struct sd *) gspca_dev;
  817. const struct cap_s *cap;
  818. int mode;
  819. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  820. cap = &capconfig[sd->sensor][mode];
  821. reg_wb(gspca_dev, 0x0900 | SQ930_CTRL_CAP_START,
  822. 0x0a00 | cap->cc_sizeid,
  823. cap->cc_bytes, 32);
  824. }
  825. static void send_stop(struct gspca_dev *gspca_dev)
  826. {
  827. reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0);
  828. }
  829. /* function called at start time before URB creation */
  830. static int sd_isoc_init(struct gspca_dev *gspca_dev)
  831. {
  832. struct sd *sd = (struct sd *) gspca_dev;
  833. gspca_dev->cam.bulk_nurbs = 1; /* there must be one URB only */
  834. sd->do_ctrl = 0;
  835. gspca_dev->cam.bulk_size = gspca_dev->pixfmt.width *
  836. gspca_dev->pixfmt.height + 8;
  837. return 0;
  838. }
  839. /* start the capture */
  840. static int sd_start(struct gspca_dev *gspca_dev)
  841. {
  842. struct sd *sd = (struct sd *) gspca_dev;
  843. int mode;
  844. bridge_init(sd);
  845. global_init(sd, 0);
  846. msleep(100);
  847. switch (sd->sensor) {
  848. case SENSOR_ICX098BQ:
  849. ucbus_write(gspca_dev, icx098bq_start_0,
  850. ARRAY_SIZE(icx098bq_start_0),
  851. 8);
  852. ucbus_write(gspca_dev, icx098bq_start_1,
  853. ARRAY_SIZE(icx098bq_start_1),
  854. 5);
  855. ucbus_write(gspca_dev, icx098bq_start_2,
  856. ARRAY_SIZE(icx098bq_start_2),
  857. 6);
  858. msleep(50);
  859. /* 1st start */
  860. send_start(gspca_dev);
  861. gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
  862. msleep(70);
  863. reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
  864. gpio_set(sd, 0x7f, 0x00ff);
  865. /* 2nd start */
  866. send_start(gspca_dev);
  867. gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
  868. goto out;
  869. case SENSOR_LZ24BP:
  870. ucbus_write(gspca_dev, lz24bp_start_0,
  871. ARRAY_SIZE(lz24bp_start_0),
  872. 8);
  873. if (sd->type != Creative_live_motion)
  874. ucbus_write(gspca_dev, lz24bp_start_1_gen,
  875. ARRAY_SIZE(lz24bp_start_1_gen),
  876. 5);
  877. else
  878. ucbus_write(gspca_dev, lz24bp_start_1_clm,
  879. ARRAY_SIZE(lz24bp_start_1_clm),
  880. 5);
  881. ucbus_write(gspca_dev, lz24bp_start_2,
  882. ARRAY_SIZE(lz24bp_start_2),
  883. 6);
  884. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  885. lz24bp_ppl(sd, mode == 1 ? 0x0564 : 0x0310);
  886. msleep(10);
  887. break;
  888. case SENSOR_MI0360:
  889. ucbus_write(gspca_dev, mi0360_start_0,
  890. ARRAY_SIZE(mi0360_start_0),
  891. 8);
  892. i2c_write(sd, mi0360_init_23,
  893. ARRAY_SIZE(mi0360_init_23));
  894. i2c_write(sd, mi0360_init_24,
  895. ARRAY_SIZE(mi0360_init_24));
  896. i2c_write(sd, mi0360_init_25,
  897. ARRAY_SIZE(mi0360_init_25));
  898. ucbus_write(gspca_dev, mi0360_start_1,
  899. ARRAY_SIZE(mi0360_start_1),
  900. 5);
  901. i2c_write(sd, mi0360_start_2,
  902. ARRAY_SIZE(mi0360_start_2));
  903. i2c_write(sd, mi0360_start_3,
  904. ARRAY_SIZE(mi0360_start_3));
  905. /* 1st start */
  906. send_start(gspca_dev);
  907. msleep(60);
  908. send_stop(gspca_dev);
  909. i2c_write(sd,
  910. mi0360_start_4, ARRAY_SIZE(mi0360_start_4));
  911. break;
  912. default:
  913. /* case SENSOR_MT9V111: */
  914. ucbus_write(gspca_dev, mi0360_start_0,
  915. ARRAY_SIZE(mi0360_start_0),
  916. 8);
  917. i2c_write(sd, mt9v111_init_0,
  918. ARRAY_SIZE(mt9v111_init_0));
  919. i2c_write(sd, mt9v111_init_1,
  920. ARRAY_SIZE(mt9v111_init_1));
  921. i2c_write(sd, mt9v111_init_2,
  922. ARRAY_SIZE(mt9v111_init_2));
  923. ucbus_write(gspca_dev, mt9v111_start_1,
  924. ARRAY_SIZE(mt9v111_start_1),
  925. 5);
  926. i2c_write(sd, mt9v111_init_3,
  927. ARRAY_SIZE(mt9v111_init_3));
  928. i2c_write(sd, mt9v111_init_4,
  929. ARRAY_SIZE(mt9v111_init_4));
  930. break;
  931. }
  932. send_start(gspca_dev);
  933. out:
  934. msleep(1000);
  935. if (sd->sensor == SENSOR_MT9V111)
  936. gpio_set(sd, SQ930_GPIO_DFL_LED, SQ930_GPIO_DFL_LED);
  937. sd->do_ctrl = 1; /* set the exposure */
  938. return gspca_dev->usb_err;
  939. }
  940. static void sd_stopN(struct gspca_dev *gspca_dev)
  941. {
  942. struct sd *sd = (struct sd *) gspca_dev;
  943. if (sd->sensor == SENSOR_MT9V111)
  944. gpio_set(sd, 0, SQ930_GPIO_DFL_LED);
  945. send_stop(gspca_dev);
  946. }
  947. /* function called when the application gets a new frame */
  948. /* It sets the exposure if required and restart the bulk transfer. */
  949. static void sd_dq_callback(struct gspca_dev *gspca_dev)
  950. {
  951. struct sd *sd = (struct sd *) gspca_dev;
  952. int ret;
  953. if (!sd->do_ctrl || gspca_dev->cam.bulk_nurbs != 0)
  954. return;
  955. sd->do_ctrl = 0;
  956. setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure),
  957. v4l2_ctrl_g_ctrl(sd->gain));
  958. gspca_dev->cam.bulk_nurbs = 1;
  959. ret = usb_submit_urb(gspca_dev->urb[0], GFP_ATOMIC);
  960. if (ret < 0)
  961. pr_err("sd_dq_callback() err %d\n", ret);
  962. /* wait a little time, otherwise the webcam crashes */
  963. msleep(100);
  964. }
  965. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  966. u8 *data, /* isoc packet */
  967. int len) /* iso packet length */
  968. {
  969. struct sd *sd = (struct sd *) gspca_dev;
  970. if (sd->do_ctrl)
  971. gspca_dev->cam.bulk_nurbs = 0;
  972. gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
  973. gspca_frame_add(gspca_dev, INTER_PACKET, data, len - 8);
  974. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  975. }
  976. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  977. {
  978. struct gspca_dev *gspca_dev =
  979. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  980. struct sd *sd = (struct sd *) gspca_dev;
  981. gspca_dev->usb_err = 0;
  982. if (!gspca_dev->streaming)
  983. return 0;
  984. switch (ctrl->id) {
  985. case V4L2_CID_EXPOSURE:
  986. setexposure(gspca_dev, ctrl->val, sd->gain->val);
  987. break;
  988. }
  989. return gspca_dev->usb_err;
  990. }
  991. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  992. .s_ctrl = sd_s_ctrl,
  993. };
  994. static int sd_init_controls(struct gspca_dev *gspca_dev)
  995. {
  996. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  997. struct sd *sd = (struct sd *) gspca_dev;
  998. gspca_dev->vdev.ctrl_handler = hdl;
  999. v4l2_ctrl_handler_init(hdl, 2);
  1000. sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1001. V4L2_CID_EXPOSURE, 1, 0xfff, 1, 0x356);
  1002. sd->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  1003. V4L2_CID_GAIN, 1, 255, 1, 0x8d);
  1004. if (hdl->error) {
  1005. pr_err("Could not initialize controls\n");
  1006. return hdl->error;
  1007. }
  1008. v4l2_ctrl_cluster(2, &sd->exposure);
  1009. return 0;
  1010. }
  1011. /* sub-driver description */
  1012. static const struct sd_desc sd_desc = {
  1013. .name = MODULE_NAME,
  1014. .config = sd_config,
  1015. .init = sd_init,
  1016. .init_controls = sd_init_controls,
  1017. .isoc_init = sd_isoc_init,
  1018. .start = sd_start,
  1019. .stopN = sd_stopN,
  1020. .pkt_scan = sd_pkt_scan,
  1021. .dq_callback = sd_dq_callback,
  1022. };
  1023. /* Table of supported USB devices */
  1024. #define ST(sensor, type) \
  1025. .driver_info = (SENSOR_ ## sensor << 8) \
  1026. | (type)
  1027. static const struct usb_device_id device_table[] = {
  1028. {USB_DEVICE(0x041e, 0x4038), ST(MI0360, 0)},
  1029. {USB_DEVICE(0x041e, 0x403c), ST(LZ24BP, 0)},
  1030. {USB_DEVICE(0x041e, 0x403d), ST(LZ24BP, 0)},
  1031. {USB_DEVICE(0x041e, 0x4041), ST(LZ24BP, Creative_live_motion)},
  1032. {USB_DEVICE(0x2770, 0x930b), ST(MI0360, 0)},
  1033. {USB_DEVICE(0x2770, 0x930c), ST(MI0360, 0)},
  1034. {}
  1035. };
  1036. MODULE_DEVICE_TABLE(usb, device_table);
  1037. /* -- device connect -- */
  1038. static int sd_probe(struct usb_interface *intf,
  1039. const struct usb_device_id *id)
  1040. {
  1041. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  1042. THIS_MODULE);
  1043. }
  1044. static struct usb_driver sd_driver = {
  1045. .name = MODULE_NAME,
  1046. .id_table = device_table,
  1047. .probe = sd_probe,
  1048. .disconnect = gspca_disconnect,
  1049. #ifdef CONFIG_PM
  1050. .suspend = gspca_suspend,
  1051. .resume = gspca_resume,
  1052. .reset_resume = gspca_resume,
  1053. #endif
  1054. };
  1055. module_usb_driver(sd_driver);