sq930x.c 32 KB

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