sq930x.c 32 KB

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