sq930x.c 37 KB

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