sq930x.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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. }
  414. static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
  415. {
  416. int ret;
  417. if (gspca_dev->usb_err < 0)
  418. return;
  419. PDEBUG(D_USBO, "reg_w v: %04x i: %04x", value, index);
  420. ret = usb_control_msg(gspca_dev->dev,
  421. usb_sndctrlpipe(gspca_dev->dev, 0),
  422. 0x0c, /* request */
  423. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  424. value, index, NULL, 0,
  425. 500);
  426. msleep(30);
  427. if (ret < 0) {
  428. pr_err("reg_w %04x %04x failed %d\n", value, index, ret);
  429. gspca_dev->usb_err = ret;
  430. }
  431. }
  432. static void reg_wb(struct gspca_dev *gspca_dev, u16 value, u16 index,
  433. const u8 *data, int len)
  434. {
  435. int ret;
  436. if (gspca_dev->usb_err < 0)
  437. return;
  438. PDEBUG(D_USBO, "reg_wb v: %04x i: %04x %02x...%02x",
  439. value, index, *data, data[len - 1]);
  440. memcpy(gspca_dev->usb_buf, data, len);
  441. ret = usb_control_msg(gspca_dev->dev,
  442. usb_sndctrlpipe(gspca_dev->dev, 0),
  443. 0x0c, /* request */
  444. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  445. value, index, gspca_dev->usb_buf, len,
  446. 1000);
  447. msleep(30);
  448. if (ret < 0) {
  449. pr_err("reg_wb %04x %04x failed %d\n", value, index, ret);
  450. gspca_dev->usb_err = ret;
  451. }
  452. }
  453. static void i2c_write(struct sd *sd,
  454. const struct i2c_write_cmd *cmd,
  455. int ncmds)
  456. {
  457. struct gspca_dev *gspca_dev = &sd->gspca_dev;
  458. const struct sensor_s *sensor;
  459. u16 val, idx;
  460. u8 *buf;
  461. int ret;
  462. if (gspca_dev->usb_err < 0)
  463. return;
  464. sensor = &sensor_tb[sd->sensor];
  465. val = (sensor->i2c_addr << 8) | SQ930_CTRL_I2C_IO;
  466. idx = (cmd->val & 0xff00) | cmd->reg;
  467. buf = gspca_dev->usb_buf;
  468. *buf++ = sensor->i2c_dum;
  469. *buf++ = cmd->val;
  470. while (--ncmds > 0) {
  471. cmd++;
  472. *buf++ = cmd->reg;
  473. *buf++ = cmd->val >> 8;
  474. *buf++ = sensor->i2c_dum;
  475. *buf++ = cmd->val;
  476. }
  477. PDEBUG(D_USBO, "i2c_w v: %04x i: %04x %02x...%02x",
  478. val, idx, gspca_dev->usb_buf[0], buf[-1]);
  479. ret = usb_control_msg(gspca_dev->dev,
  480. usb_sndctrlpipe(gspca_dev->dev, 0),
  481. 0x0c, /* request */
  482. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  483. val, idx,
  484. gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
  485. 500);
  486. if (ret < 0) {
  487. pr_err("i2c_write failed %d\n", ret);
  488. gspca_dev->usb_err = ret;
  489. }
  490. }
  491. static void ucbus_write(struct gspca_dev *gspca_dev,
  492. const struct ucbus_write_cmd *cmd,
  493. int ncmds,
  494. int batchsize)
  495. {
  496. u8 *buf;
  497. u16 val, idx;
  498. int len, ret;
  499. if (gspca_dev->usb_err < 0)
  500. return;
  501. if ((batchsize - 1) * 3 > USB_BUF_SZ) {
  502. PERR("Bug: usb_buf overflow\n");
  503. gspca_dev->usb_err = -ENOMEM;
  504. return;
  505. }
  506. for (;;) {
  507. len = ncmds;
  508. if (len > batchsize)
  509. len = batchsize;
  510. ncmds -= len;
  511. val = (cmd->bw_addr << 8) | SQ930_CTRL_UCBUS_IO;
  512. idx = (cmd->bw_data << 8) | (cmd->bw_addr >> 8);
  513. buf = gspca_dev->usb_buf;
  514. while (--len > 0) {
  515. cmd++;
  516. *buf++ = cmd->bw_addr;
  517. *buf++ = cmd->bw_addr >> 8;
  518. *buf++ = cmd->bw_data;
  519. }
  520. if (buf != gspca_dev->usb_buf)
  521. PDEBUG(D_USBO, "ucbus v: %04x i: %04x %02x...%02x",
  522. val, idx,
  523. gspca_dev->usb_buf[0], buf[-1]);
  524. else
  525. PDEBUG(D_USBO, "ucbus v: %04x i: %04x",
  526. val, idx);
  527. ret = usb_control_msg(gspca_dev->dev,
  528. usb_sndctrlpipe(gspca_dev->dev, 0),
  529. 0x0c, /* request */
  530. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  531. val, idx,
  532. gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
  533. 500);
  534. if (ret < 0) {
  535. pr_err("ucbus_write failed %d\n", ret);
  536. gspca_dev->usb_err = ret;
  537. return;
  538. }
  539. msleep(30);
  540. if (ncmds <= 0)
  541. break;
  542. cmd++;
  543. }
  544. }
  545. static void gpio_set(struct sd *sd, u16 val, u16 mask)
  546. {
  547. struct gspca_dev *gspca_dev = &sd->gspca_dev;
  548. if (mask & 0x00ff) {
  549. sd->gpio[0] &= ~mask;
  550. sd->gpio[0] |= val;
  551. reg_w(gspca_dev, 0x0100 | SQ930_CTRL_GPIO,
  552. ~sd->gpio[0] << 8);
  553. }
  554. mask >>= 8;
  555. val >>= 8;
  556. if (mask) {
  557. sd->gpio[1] &= ~mask;
  558. sd->gpio[1] |= val;
  559. reg_w(gspca_dev, 0x0300 | SQ930_CTRL_GPIO,
  560. ~sd->gpio[1] << 8);
  561. }
  562. }
  563. static void gpio_init(struct sd *sd,
  564. const u8 *gpio)
  565. {
  566. gpio_set(sd, *gpio++, 0x000f);
  567. gpio_set(sd, *gpio++, 0x000f);
  568. gpio_set(sd, *gpio++, 0x000f);
  569. gpio_set(sd, *gpio++, 0x000f);
  570. gpio_set(sd, *gpio, 0x000f);
  571. }
  572. static void bridge_init(struct sd *sd)
  573. {
  574. static const struct ucbus_write_cmd clkfreq_cmd = {
  575. 0xf031, 0 /* SQ930_CLKFREQ_60MHZ */
  576. };
  577. ucbus_write(&sd->gspca_dev, &clkfreq_cmd, 1, 1);
  578. gpio_set(sd, SQ930_GPIO_POWER, 0xff00);
  579. }
  580. static void cmos_probe(struct gspca_dev *gspca_dev)
  581. {
  582. struct sd *sd = (struct sd *) gspca_dev;
  583. int i;
  584. const struct sensor_s *sensor;
  585. static const u8 probe_order[] = {
  586. /* SENSOR_LZ24BP, (tested as ccd) */
  587. SENSOR_OV9630,
  588. SENSOR_MI0360,
  589. SENSOR_OV7660,
  590. SENSOR_MT9V111,
  591. };
  592. for (i = 0; i < ARRAY_SIZE(probe_order); i++) {
  593. sensor = &sensor_tb[probe_order[i]];
  594. ucbus_write(&sd->gspca_dev, sensor->cmd, sensor->cmd_len, 8);
  595. gpio_init(sd, sensor->gpio);
  596. msleep(100);
  597. reg_r(gspca_dev, (sensor->i2c_addr << 8) | 0x001c, 1);
  598. msleep(100);
  599. if (gspca_dev->usb_buf[0] != 0)
  600. break;
  601. }
  602. if (i >= ARRAY_SIZE(probe_order)) {
  603. pr_err("Unknown sensor\n");
  604. gspca_dev->usb_err = -EINVAL;
  605. return;
  606. }
  607. sd->sensor = probe_order[i];
  608. switch (sd->sensor) {
  609. case SENSOR_OV7660:
  610. case SENSOR_OV9630:
  611. pr_err("Sensor %s not yet treated\n",
  612. sensor_tb[sd->sensor].name);
  613. gspca_dev->usb_err = -EINVAL;
  614. break;
  615. }
  616. }
  617. static void mt9v111_init(struct gspca_dev *gspca_dev)
  618. {
  619. int i, nwait;
  620. static const u8 cmd_001b[] = {
  621. 0x00, 0x3b, 0xf6, 0x01, 0x03, 0x02, 0x00, 0x00,
  622. 0x00, 0x00, 0x00
  623. };
  624. static const u8 cmd_011b[][7] = {
  625. {0x10, 0x01, 0x66, 0x08, 0x00, 0x00, 0x00},
  626. {0x01, 0x00, 0x1a, 0x04, 0x00, 0x00, 0x00},
  627. {0x20, 0x00, 0x10, 0x04, 0x00, 0x00, 0x00},
  628. {0x02, 0x01, 0xae, 0x01, 0x00, 0x00, 0x00},
  629. };
  630. reg_wb(gspca_dev, 0x001b, 0x0000, cmd_001b, sizeof cmd_001b);
  631. for (i = 0; i < ARRAY_SIZE(cmd_011b); i++) {
  632. reg_wb(gspca_dev, 0x001b, 0x0000, cmd_011b[i],
  633. ARRAY_SIZE(cmd_011b[0]));
  634. msleep(400);
  635. nwait = 20;
  636. for (;;) {
  637. reg_r(gspca_dev, 0x031b, 1);
  638. if (gspca_dev->usb_buf[0] == 0
  639. || gspca_dev->usb_err != 0)
  640. break;
  641. if (--nwait < 0) {
  642. PDEBUG(D_PROBE, "mt9v111_init timeout");
  643. gspca_dev->usb_err = -ETIME;
  644. return;
  645. }
  646. msleep(50);
  647. }
  648. }
  649. }
  650. static void global_init(struct sd *sd, int first_time)
  651. {
  652. switch (sd->sensor) {
  653. case SENSOR_ICX098BQ:
  654. if (first_time)
  655. ucbus_write(&sd->gspca_dev,
  656. icx098bq_start_0,
  657. 8, 8);
  658. gpio_init(sd, sensor_tb[sd->sensor].gpio);
  659. break;
  660. case SENSOR_LZ24BP:
  661. if (sd->type != Creative_live_motion)
  662. gpio_set(sd, SQ930_GPIO_EXTRA1, 0x00ff);
  663. else
  664. gpio_set(sd, 0, 0x00ff);
  665. msleep(50);
  666. if (first_time)
  667. ucbus_write(&sd->gspca_dev,
  668. lz24bp_start_0,
  669. 8, 8);
  670. gpio_init(sd, sensor_tb[sd->sensor].gpio);
  671. break;
  672. case SENSOR_MI0360:
  673. if (first_time)
  674. ucbus_write(&sd->gspca_dev,
  675. mi0360_start_0,
  676. ARRAY_SIZE(mi0360_start_0),
  677. 8);
  678. gpio_init(sd, sensor_tb[sd->sensor].gpio);
  679. gpio_set(sd, SQ930_GPIO_EXTRA2, SQ930_GPIO_EXTRA2);
  680. break;
  681. default:
  682. /* case SENSOR_MT9V111: */
  683. if (first_time)
  684. mt9v111_init(&sd->gspca_dev);
  685. else
  686. gpio_init(sd, sensor_tb[sd->sensor].gpio);
  687. break;
  688. }
  689. }
  690. static void lz24bp_ppl(struct sd *sd, u16 ppl)
  691. {
  692. struct ucbus_write_cmd cmds[2] = {
  693. {0xf810, ppl >> 8},
  694. {0xf811, ppl}
  695. };
  696. ucbus_write(&sd->gspca_dev, cmds, ARRAY_SIZE(cmds), 2);
  697. }
  698. static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 gain)
  699. {
  700. struct sd *sd = (struct sd *) gspca_dev;
  701. int i, integclks, intstartclk, frameclks, min_frclk;
  702. const struct sensor_s *sensor;
  703. u16 cmd;
  704. u8 buf[15];
  705. integclks = expo;
  706. i = 0;
  707. cmd = SQ930_CTRL_SET_EXPOSURE;
  708. switch (sd->sensor) {
  709. case SENSOR_ICX098BQ: /* ccd */
  710. case SENSOR_LZ24BP:
  711. min_frclk = sd->sensor == SENSOR_ICX098BQ ? 0x210 : 0x26f;
  712. if (integclks >= min_frclk) {
  713. intstartclk = 0;
  714. frameclks = integclks;
  715. } else {
  716. intstartclk = min_frclk - integclks;
  717. frameclks = min_frclk;
  718. }
  719. buf[i++] = intstartclk >> 8;
  720. buf[i++] = intstartclk;
  721. buf[i++] = frameclks >> 8;
  722. buf[i++] = frameclks;
  723. buf[i++] = gain;
  724. break;
  725. default: /* cmos */
  726. /* case SENSOR_MI0360: */
  727. /* case SENSOR_MT9V111: */
  728. cmd |= 0x0100;
  729. sensor = &sensor_tb[sd->sensor];
  730. buf[i++] = sensor->i2c_addr; /* i2c_slave_addr */
  731. buf[i++] = 0x08; /* 2 * ni2c */
  732. buf[i++] = 0x09; /* reg = shutter width */
  733. buf[i++] = integclks >> 8; /* val H */
  734. buf[i++] = sensor->i2c_dum;
  735. buf[i++] = integclks; /* val L */
  736. buf[i++] = 0x35; /* reg = global gain */
  737. buf[i++] = 0x00; /* val H */
  738. buf[i++] = sensor->i2c_dum;
  739. buf[i++] = 0x80 + gain / 2; /* val L */
  740. buf[i++] = 0x00;
  741. buf[i++] = 0x00;
  742. buf[i++] = 0x00;
  743. buf[i++] = 0x00;
  744. buf[i++] = 0x83;
  745. break;
  746. }
  747. reg_wb(gspca_dev, cmd, 0, buf, i);
  748. }
  749. /* This function is called at probe time just before sd_init */
  750. static int sd_config(struct gspca_dev *gspca_dev,
  751. const struct usb_device_id *id)
  752. {
  753. struct sd *sd = (struct sd *) gspca_dev;
  754. struct cam *cam = &gspca_dev->cam;
  755. sd->sensor = id->driver_info >> 8;
  756. sd->type = id->driver_info;
  757. cam->cam_mode = vga_mode;
  758. cam->nmodes = ARRAY_SIZE(vga_mode);
  759. cam->bulk = 1;
  760. return 0;
  761. }
  762. /* this function is called at probe and resume time */
  763. static int sd_init(struct gspca_dev *gspca_dev)
  764. {
  765. struct sd *sd = (struct sd *) gspca_dev;
  766. sd->gpio[0] = sd->gpio[1] = 0xff; /* force gpio rewrite */
  767. /*fixme: is this needed for icx098bp and mi0360?
  768. if (sd->sensor != SENSOR_LZ24BP)
  769. reg_w(gspca_dev, SQ930_CTRL_RESET, 0x0000);
  770. */
  771. reg_r(gspca_dev, SQ930_CTRL_GET_DEV_INFO, 8);
  772. if (gspca_dev->usb_err < 0)
  773. return gspca_dev->usb_err;
  774. /* it returns:
  775. * 03 00 12 93 0b f6 c9 00 live! ultra
  776. * 03 00 07 93 0b f6 ca 00 live! ultra for notebook
  777. * 03 00 12 93 0b fe c8 00 Trust WB-3500T
  778. * 02 00 06 93 0b fe c8 00 Joy-IT 318S
  779. * 03 00 12 93 0b f6 cf 00 icam tracer - sensor icx098bq
  780. * 02 00 12 93 0b fe cf 00 ProQ Motion Webcam
  781. *
  782. * byte
  783. * 0: 02 = usb 1.0 (12Mbit) / 03 = usb2.0 (480Mbit)
  784. * 1: 00
  785. * 2: 06 / 07 / 12 = mode webcam? firmware??
  786. * 3: 93 chip = 930b (930b or 930c)
  787. * 4: 0b
  788. * 5: f6 = cdd (icx098bq, lz24bp) / fe or de = cmos (i2c) (other sensors)
  789. * 6: c8 / c9 / ca / cf = mode webcam?, sensor? webcam?
  790. * 7: 00
  791. */
  792. PDEBUG(D_PROBE, "info: %*ph", 8, gspca_dev->usb_buf);
  793. bridge_init(sd);
  794. if (sd->sensor == SENSOR_MI0360) {
  795. /* no sensor probe for icam tracer */
  796. if (gspca_dev->usb_buf[5] == 0xf6) /* if ccd */
  797. sd->sensor = SENSOR_ICX098BQ;
  798. else
  799. cmos_probe(gspca_dev);
  800. }
  801. if (gspca_dev->usb_err >= 0) {
  802. PDEBUG(D_PROBE, "Sensor %s", sensor_tb[sd->sensor].name);
  803. global_init(sd, 1);
  804. }
  805. return gspca_dev->usb_err;
  806. }
  807. /* send the start/stop commands to the webcam */
  808. static void send_start(struct gspca_dev *gspca_dev)
  809. {
  810. struct sd *sd = (struct sd *) gspca_dev;
  811. const struct cap_s *cap;
  812. int mode;
  813. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  814. cap = &capconfig[sd->sensor][mode];
  815. reg_wb(gspca_dev, 0x0900 | SQ930_CTRL_CAP_START,
  816. 0x0a00 | cap->cc_sizeid,
  817. cap->cc_bytes, 32);
  818. }
  819. static void send_stop(struct gspca_dev *gspca_dev)
  820. {
  821. reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0);
  822. }
  823. /* function called at start time before URB creation */
  824. static int sd_isoc_init(struct gspca_dev *gspca_dev)
  825. {
  826. struct sd *sd = (struct sd *) gspca_dev;
  827. gspca_dev->cam.bulk_nurbs = 1; /* there must be one URB only */
  828. sd->do_ctrl = 0;
  829. gspca_dev->cam.bulk_size = gspca_dev->pixfmt.width *
  830. gspca_dev->pixfmt.height + 8;
  831. return 0;
  832. }
  833. /* start the capture */
  834. static int sd_start(struct gspca_dev *gspca_dev)
  835. {
  836. struct sd *sd = (struct sd *) gspca_dev;
  837. int mode;
  838. bridge_init(sd);
  839. global_init(sd, 0);
  840. msleep(100);
  841. switch (sd->sensor) {
  842. case SENSOR_ICX098BQ:
  843. ucbus_write(gspca_dev, icx098bq_start_0,
  844. ARRAY_SIZE(icx098bq_start_0),
  845. 8);
  846. ucbus_write(gspca_dev, icx098bq_start_1,
  847. ARRAY_SIZE(icx098bq_start_1),
  848. 5);
  849. ucbus_write(gspca_dev, icx098bq_start_2,
  850. ARRAY_SIZE(icx098bq_start_2),
  851. 6);
  852. msleep(50);
  853. /* 1st start */
  854. send_start(gspca_dev);
  855. gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
  856. msleep(70);
  857. reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
  858. gpio_set(sd, 0x7f, 0x00ff);
  859. /* 2nd start */
  860. send_start(gspca_dev);
  861. gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
  862. goto out;
  863. case SENSOR_LZ24BP:
  864. ucbus_write(gspca_dev, lz24bp_start_0,
  865. ARRAY_SIZE(lz24bp_start_0),
  866. 8);
  867. if (sd->type != Creative_live_motion)
  868. ucbus_write(gspca_dev, lz24bp_start_1_gen,
  869. ARRAY_SIZE(lz24bp_start_1_gen),
  870. 5);
  871. else
  872. ucbus_write(gspca_dev, lz24bp_start_1_clm,
  873. ARRAY_SIZE(lz24bp_start_1_clm),
  874. 5);
  875. ucbus_write(gspca_dev, lz24bp_start_2,
  876. ARRAY_SIZE(lz24bp_start_2),
  877. 6);
  878. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  879. lz24bp_ppl(sd, mode == 1 ? 0x0564 : 0x0310);
  880. msleep(10);
  881. break;
  882. case SENSOR_MI0360:
  883. ucbus_write(gspca_dev, mi0360_start_0,
  884. ARRAY_SIZE(mi0360_start_0),
  885. 8);
  886. i2c_write(sd, mi0360_init_23,
  887. ARRAY_SIZE(mi0360_init_23));
  888. i2c_write(sd, mi0360_init_24,
  889. ARRAY_SIZE(mi0360_init_24));
  890. i2c_write(sd, mi0360_init_25,
  891. ARRAY_SIZE(mi0360_init_25));
  892. ucbus_write(gspca_dev, mi0360_start_1,
  893. ARRAY_SIZE(mi0360_start_1),
  894. 5);
  895. i2c_write(sd, mi0360_start_2,
  896. ARRAY_SIZE(mi0360_start_2));
  897. i2c_write(sd, mi0360_start_3,
  898. ARRAY_SIZE(mi0360_start_3));
  899. /* 1st start */
  900. send_start(gspca_dev);
  901. msleep(60);
  902. send_stop(gspca_dev);
  903. i2c_write(sd,
  904. mi0360_start_4, ARRAY_SIZE(mi0360_start_4));
  905. break;
  906. default:
  907. /* case SENSOR_MT9V111: */
  908. ucbus_write(gspca_dev, mi0360_start_0,
  909. ARRAY_SIZE(mi0360_start_0),
  910. 8);
  911. i2c_write(sd, mt9v111_init_0,
  912. ARRAY_SIZE(mt9v111_init_0));
  913. i2c_write(sd, mt9v111_init_1,
  914. ARRAY_SIZE(mt9v111_init_1));
  915. i2c_write(sd, mt9v111_init_2,
  916. ARRAY_SIZE(mt9v111_init_2));
  917. ucbus_write(gspca_dev, mt9v111_start_1,
  918. ARRAY_SIZE(mt9v111_start_1),
  919. 5);
  920. i2c_write(sd, mt9v111_init_3,
  921. ARRAY_SIZE(mt9v111_init_3));
  922. i2c_write(sd, mt9v111_init_4,
  923. ARRAY_SIZE(mt9v111_init_4));
  924. break;
  925. }
  926. send_start(gspca_dev);
  927. out:
  928. msleep(1000);
  929. if (sd->sensor == SENSOR_MT9V111)
  930. gpio_set(sd, SQ930_GPIO_DFL_LED, SQ930_GPIO_DFL_LED);
  931. sd->do_ctrl = 1; /* set the exposure */
  932. return gspca_dev->usb_err;
  933. }
  934. static void sd_stopN(struct gspca_dev *gspca_dev)
  935. {
  936. struct sd *sd = (struct sd *) gspca_dev;
  937. if (sd->sensor == SENSOR_MT9V111)
  938. gpio_set(sd, 0, SQ930_GPIO_DFL_LED);
  939. send_stop(gspca_dev);
  940. }
  941. /* function called when the application gets a new frame */
  942. /* It sets the exposure if required and restart the bulk transfer. */
  943. static void sd_dq_callback(struct gspca_dev *gspca_dev)
  944. {
  945. struct sd *sd = (struct sd *) gspca_dev;
  946. int ret;
  947. if (!sd->do_ctrl || gspca_dev->cam.bulk_nurbs != 0)
  948. return;
  949. sd->do_ctrl = 0;
  950. setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure),
  951. v4l2_ctrl_g_ctrl(sd->gain));
  952. gspca_dev->cam.bulk_nurbs = 1;
  953. ret = usb_submit_urb(gspca_dev->urb[0], GFP_ATOMIC);
  954. if (ret < 0)
  955. pr_err("sd_dq_callback() err %d\n", ret);
  956. /* wait a little time, otherwise the webcam crashes */
  957. msleep(100);
  958. }
  959. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  960. u8 *data, /* isoc packet */
  961. int len) /* iso packet length */
  962. {
  963. struct sd *sd = (struct sd *) gspca_dev;
  964. if (sd->do_ctrl)
  965. gspca_dev->cam.bulk_nurbs = 0;
  966. gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
  967. gspca_frame_add(gspca_dev, INTER_PACKET, data, len - 8);
  968. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  969. }
  970. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  971. {
  972. struct gspca_dev *gspca_dev =
  973. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  974. struct sd *sd = (struct sd *) gspca_dev;
  975. gspca_dev->usb_err = 0;
  976. if (!gspca_dev->streaming)
  977. return 0;
  978. switch (ctrl->id) {
  979. case V4L2_CID_EXPOSURE:
  980. setexposure(gspca_dev, ctrl->val, sd->gain->val);
  981. break;
  982. }
  983. return gspca_dev->usb_err;
  984. }
  985. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  986. .s_ctrl = sd_s_ctrl,
  987. };
  988. static int sd_init_controls(struct gspca_dev *gspca_dev)
  989. {
  990. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  991. struct sd *sd = (struct sd *) gspca_dev;
  992. gspca_dev->vdev.ctrl_handler = hdl;
  993. v4l2_ctrl_handler_init(hdl, 2);
  994. sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  995. V4L2_CID_EXPOSURE, 1, 0xfff, 1, 0x356);
  996. sd->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  997. V4L2_CID_GAIN, 1, 255, 1, 0x8d);
  998. if (hdl->error) {
  999. pr_err("Could not initialize controls\n");
  1000. return hdl->error;
  1001. }
  1002. v4l2_ctrl_cluster(2, &sd->exposure);
  1003. return 0;
  1004. }
  1005. /* sub-driver description */
  1006. static const struct sd_desc sd_desc = {
  1007. .name = MODULE_NAME,
  1008. .config = sd_config,
  1009. .init = sd_init,
  1010. .init_controls = sd_init_controls,
  1011. .isoc_init = sd_isoc_init,
  1012. .start = sd_start,
  1013. .stopN = sd_stopN,
  1014. .pkt_scan = sd_pkt_scan,
  1015. .dq_callback = sd_dq_callback,
  1016. };
  1017. /* Table of supported USB devices */
  1018. #define ST(sensor, type) \
  1019. .driver_info = (SENSOR_ ## sensor << 8) \
  1020. | (type)
  1021. static const struct usb_device_id device_table[] = {
  1022. {USB_DEVICE(0x041e, 0x4038), ST(MI0360, 0)},
  1023. {USB_DEVICE(0x041e, 0x403c), ST(LZ24BP, 0)},
  1024. {USB_DEVICE(0x041e, 0x403d), ST(LZ24BP, 0)},
  1025. {USB_DEVICE(0x041e, 0x4041), ST(LZ24BP, Creative_live_motion)},
  1026. {USB_DEVICE(0x2770, 0x930b), ST(MI0360, 0)},
  1027. {USB_DEVICE(0x2770, 0x930c), ST(MI0360, 0)},
  1028. {}
  1029. };
  1030. MODULE_DEVICE_TABLE(usb, device_table);
  1031. /* -- device connect -- */
  1032. static int sd_probe(struct usb_interface *intf,
  1033. const struct usb_device_id *id)
  1034. {
  1035. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  1036. THIS_MODULE);
  1037. }
  1038. static struct usb_driver sd_driver = {
  1039. .name = MODULE_NAME,
  1040. .id_table = device_table,
  1041. .probe = sd_probe,
  1042. .disconnect = gspca_disconnect,
  1043. #ifdef CONFIG_PM
  1044. .suspend = gspca_suspend,
  1045. .resume = gspca_resume,
  1046. .reset_resume = gspca_resume,
  1047. #endif
  1048. };
  1049. module_usb_driver(sd_driver);