sq930x.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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. gspca_dbg(gspca_dev, D_USBO, "reg_w v: %04x i: %04x\n", 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. gspca_dbg(gspca_dev, D_USBO, "reg_wb v: %04x i: %04x %02x...%02x\n",
  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. gspca_dbg(gspca_dev, D_USBO, "i2c_w v: %04x i: %04x %02x...%02x\n",
  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. gspca_err(gspca_dev, "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. gspca_dbg(gspca_dev, D_USBO, "ucbus v: %04x i: %04x %02x...%02x\n",
  522. val, idx,
  523. gspca_dev->usb_buf[0], buf[-1]);
  524. else
  525. gspca_dbg(gspca_dev, D_USBO, "ucbus v: %04x i: %04x\n",
  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. gspca_dbg(gspca_dev, D_PROBE, "mt9v111_init timeout\n");
  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. gspca_dbg(gspca_dev, D_PROBE, "info: %*ph\n", 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. gspca_dbg(gspca_dev, D_PROBE, "Sensor %s\n",
  803. sensor_tb[sd->sensor].name);
  804. global_init(sd, 1);
  805. }
  806. return gspca_dev->usb_err;
  807. }
  808. /* send the start/stop commands to the webcam */
  809. static void send_start(struct gspca_dev *gspca_dev)
  810. {
  811. struct sd *sd = (struct sd *) gspca_dev;
  812. const struct cap_s *cap;
  813. int mode;
  814. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  815. cap = &capconfig[sd->sensor][mode];
  816. reg_wb(gspca_dev, 0x0900 | SQ930_CTRL_CAP_START,
  817. 0x0a00 | cap->cc_sizeid,
  818. cap->cc_bytes, 32);
  819. }
  820. static void send_stop(struct gspca_dev *gspca_dev)
  821. {
  822. reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0);
  823. }
  824. /* function called at start time before URB creation */
  825. static int sd_isoc_init(struct gspca_dev *gspca_dev)
  826. {
  827. struct sd *sd = (struct sd *) gspca_dev;
  828. gspca_dev->cam.bulk_nurbs = 1; /* there must be one URB only */
  829. sd->do_ctrl = 0;
  830. gspca_dev->cam.bulk_size = gspca_dev->pixfmt.width *
  831. gspca_dev->pixfmt.height + 8;
  832. return 0;
  833. }
  834. /* start the capture */
  835. static int sd_start(struct gspca_dev *gspca_dev)
  836. {
  837. struct sd *sd = (struct sd *) gspca_dev;
  838. int mode;
  839. bridge_init(sd);
  840. global_init(sd, 0);
  841. msleep(100);
  842. switch (sd->sensor) {
  843. case SENSOR_ICX098BQ:
  844. ucbus_write(gspca_dev, icx098bq_start_0,
  845. ARRAY_SIZE(icx098bq_start_0),
  846. 8);
  847. ucbus_write(gspca_dev, icx098bq_start_1,
  848. ARRAY_SIZE(icx098bq_start_1),
  849. 5);
  850. ucbus_write(gspca_dev, icx098bq_start_2,
  851. ARRAY_SIZE(icx098bq_start_2),
  852. 6);
  853. msleep(50);
  854. /* 1st start */
  855. send_start(gspca_dev);
  856. gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
  857. msleep(70);
  858. reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
  859. gpio_set(sd, 0x7f, 0x00ff);
  860. /* 2nd start */
  861. send_start(gspca_dev);
  862. gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
  863. goto out;
  864. case SENSOR_LZ24BP:
  865. ucbus_write(gspca_dev, lz24bp_start_0,
  866. ARRAY_SIZE(lz24bp_start_0),
  867. 8);
  868. if (sd->type != Creative_live_motion)
  869. ucbus_write(gspca_dev, lz24bp_start_1_gen,
  870. ARRAY_SIZE(lz24bp_start_1_gen),
  871. 5);
  872. else
  873. ucbus_write(gspca_dev, lz24bp_start_1_clm,
  874. ARRAY_SIZE(lz24bp_start_1_clm),
  875. 5);
  876. ucbus_write(gspca_dev, lz24bp_start_2,
  877. ARRAY_SIZE(lz24bp_start_2),
  878. 6);
  879. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  880. lz24bp_ppl(sd, mode == 1 ? 0x0564 : 0x0310);
  881. msleep(10);
  882. break;
  883. case SENSOR_MI0360:
  884. ucbus_write(gspca_dev, mi0360_start_0,
  885. ARRAY_SIZE(mi0360_start_0),
  886. 8);
  887. i2c_write(sd, mi0360_init_23,
  888. ARRAY_SIZE(mi0360_init_23));
  889. i2c_write(sd, mi0360_init_24,
  890. ARRAY_SIZE(mi0360_init_24));
  891. i2c_write(sd, mi0360_init_25,
  892. ARRAY_SIZE(mi0360_init_25));
  893. ucbus_write(gspca_dev, mi0360_start_1,
  894. ARRAY_SIZE(mi0360_start_1),
  895. 5);
  896. i2c_write(sd, mi0360_start_2,
  897. ARRAY_SIZE(mi0360_start_2));
  898. i2c_write(sd, mi0360_start_3,
  899. ARRAY_SIZE(mi0360_start_3));
  900. /* 1st start */
  901. send_start(gspca_dev);
  902. msleep(60);
  903. send_stop(gspca_dev);
  904. i2c_write(sd,
  905. mi0360_start_4, ARRAY_SIZE(mi0360_start_4));
  906. break;
  907. default:
  908. /* case SENSOR_MT9V111: */
  909. ucbus_write(gspca_dev, mi0360_start_0,
  910. ARRAY_SIZE(mi0360_start_0),
  911. 8);
  912. i2c_write(sd, mt9v111_init_0,
  913. ARRAY_SIZE(mt9v111_init_0));
  914. i2c_write(sd, mt9v111_init_1,
  915. ARRAY_SIZE(mt9v111_init_1));
  916. i2c_write(sd, mt9v111_init_2,
  917. ARRAY_SIZE(mt9v111_init_2));
  918. ucbus_write(gspca_dev, mt9v111_start_1,
  919. ARRAY_SIZE(mt9v111_start_1),
  920. 5);
  921. i2c_write(sd, mt9v111_init_3,
  922. ARRAY_SIZE(mt9v111_init_3));
  923. i2c_write(sd, mt9v111_init_4,
  924. ARRAY_SIZE(mt9v111_init_4));
  925. break;
  926. }
  927. send_start(gspca_dev);
  928. out:
  929. msleep(1000);
  930. if (sd->sensor == SENSOR_MT9V111)
  931. gpio_set(sd, SQ930_GPIO_DFL_LED, SQ930_GPIO_DFL_LED);
  932. sd->do_ctrl = 1; /* set the exposure */
  933. return gspca_dev->usb_err;
  934. }
  935. static void sd_stopN(struct gspca_dev *gspca_dev)
  936. {
  937. struct sd *sd = (struct sd *) gspca_dev;
  938. if (sd->sensor == SENSOR_MT9V111)
  939. gpio_set(sd, 0, SQ930_GPIO_DFL_LED);
  940. send_stop(gspca_dev);
  941. }
  942. /* function called when the application gets a new frame */
  943. /* It sets the exposure if required and restart the bulk transfer. */
  944. static void sd_dq_callback(struct gspca_dev *gspca_dev)
  945. {
  946. struct sd *sd = (struct sd *) gspca_dev;
  947. int ret;
  948. if (!sd->do_ctrl || gspca_dev->cam.bulk_nurbs != 0)
  949. return;
  950. sd->do_ctrl = 0;
  951. setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure),
  952. v4l2_ctrl_g_ctrl(sd->gain));
  953. gspca_dev->cam.bulk_nurbs = 1;
  954. ret = usb_submit_urb(gspca_dev->urb[0], GFP_ATOMIC);
  955. if (ret < 0)
  956. pr_err("sd_dq_callback() err %d\n", ret);
  957. /* wait a little time, otherwise the webcam crashes */
  958. msleep(100);
  959. }
  960. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  961. u8 *data, /* isoc packet */
  962. int len) /* iso packet length */
  963. {
  964. struct sd *sd = (struct sd *) gspca_dev;
  965. if (sd->do_ctrl)
  966. gspca_dev->cam.bulk_nurbs = 0;
  967. gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
  968. gspca_frame_add(gspca_dev, INTER_PACKET, data, len - 8);
  969. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  970. }
  971. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  972. {
  973. struct gspca_dev *gspca_dev =
  974. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  975. struct sd *sd = (struct sd *) gspca_dev;
  976. gspca_dev->usb_err = 0;
  977. if (!gspca_dev->streaming)
  978. return 0;
  979. switch (ctrl->id) {
  980. case V4L2_CID_EXPOSURE:
  981. setexposure(gspca_dev, ctrl->val, sd->gain->val);
  982. break;
  983. }
  984. return gspca_dev->usb_err;
  985. }
  986. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  987. .s_ctrl = sd_s_ctrl,
  988. };
  989. static int sd_init_controls(struct gspca_dev *gspca_dev)
  990. {
  991. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  992. struct sd *sd = (struct sd *) gspca_dev;
  993. gspca_dev->vdev.ctrl_handler = hdl;
  994. v4l2_ctrl_handler_init(hdl, 2);
  995. sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  996. V4L2_CID_EXPOSURE, 1, 0xfff, 1, 0x356);
  997. sd->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  998. V4L2_CID_GAIN, 1, 255, 1, 0x8d);
  999. if (hdl->error) {
  1000. pr_err("Could not initialize controls\n");
  1001. return hdl->error;
  1002. }
  1003. v4l2_ctrl_cluster(2, &sd->exposure);
  1004. return 0;
  1005. }
  1006. /* sub-driver description */
  1007. static const struct sd_desc sd_desc = {
  1008. .name = MODULE_NAME,
  1009. .config = sd_config,
  1010. .init = sd_init,
  1011. .init_controls = sd_init_controls,
  1012. .isoc_init = sd_isoc_init,
  1013. .start = sd_start,
  1014. .stopN = sd_stopN,
  1015. .pkt_scan = sd_pkt_scan,
  1016. .dq_callback = sd_dq_callback,
  1017. };
  1018. /* Table of supported USB devices */
  1019. #define ST(sensor, type) \
  1020. .driver_info = (SENSOR_ ## sensor << 8) \
  1021. | (type)
  1022. static const struct usb_device_id device_table[] = {
  1023. {USB_DEVICE(0x041e, 0x4038), ST(MI0360, 0)},
  1024. {USB_DEVICE(0x041e, 0x403c), ST(LZ24BP, 0)},
  1025. {USB_DEVICE(0x041e, 0x403d), ST(LZ24BP, 0)},
  1026. {USB_DEVICE(0x041e, 0x4041), ST(LZ24BP, Creative_live_motion)},
  1027. {USB_DEVICE(0x2770, 0x930b), ST(MI0360, 0)},
  1028. {USB_DEVICE(0x2770, 0x930c), ST(MI0360, 0)},
  1029. {}
  1030. };
  1031. MODULE_DEVICE_TABLE(usb, device_table);
  1032. /* -- device connect -- */
  1033. static int sd_probe(struct usb_interface *intf,
  1034. const struct usb_device_id *id)
  1035. {
  1036. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  1037. THIS_MODULE);
  1038. }
  1039. static struct usb_driver sd_driver = {
  1040. .name = MODULE_NAME,
  1041. .id_table = device_table,
  1042. .probe = sd_probe,
  1043. .disconnect = gspca_disconnect,
  1044. #ifdef CONFIG_PM
  1045. .suspend = gspca_suspend,
  1046. .resume = gspca_resume,
  1047. .reset_resume = gspca_resume,
  1048. #endif
  1049. };
  1050. module_usb_driver(sd_driver);