sq930x.c 32 KB

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