sq930x.c 32 KB

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