sq930x.c 32 KB

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