sq930x.c 31 KB

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