sq930x.c 31 KB

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