touptek.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * ToupTek UCMOS / AmScope MU series camera driver
  3. * TODO: contrast with ScopeTek / AmScope MDC cameras
  4. *
  5. * Copyright (C) 2012-2014 John McMaster <JohnDMcMaster@gmail.com>
  6. *
  7. * Special thanks to Bushing for helping with the decrypt algorithm and
  8. * Sean O'Sullivan / the Rensselaer Center for Open Source
  9. * Software (RCOS) for helping me learn kernel development
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. */
  21. #include "gspca.h"
  22. #define MODULE_NAME "touptek"
  23. MODULE_AUTHOR("John McMaster");
  24. MODULE_DESCRIPTION("ToupTek UCMOS / Amscope MU microscope camera driver");
  25. MODULE_LICENSE("GPL");
  26. /*
  27. * Exposure reg is linear with exposure time
  28. * Exposure (sec), E (reg)
  29. * 0.000400, 0x0002
  30. * 0.001000, 0x0005
  31. * 0.005000, 0x0019
  32. * 0.020000, 0x0064
  33. * 0.080000, 0x0190
  34. * 0.400000, 0x07D0
  35. * 1.000000, 0x1388
  36. * 2.000000, 0x2710
  37. *
  38. * Three gain stages
  39. * 0x1000: master channel enable bit
  40. * 0x007F: low gain bits
  41. * 0x0080: medium gain bit
  42. * 0x0100: high gain bit
  43. * gain = enable * (1 + regH) * (1 + regM) * z * regL
  44. *
  45. * Gain implementation
  46. * Want to do something similar to mt9v011.c's set_balance
  47. *
  48. * Gain does not vary with resolution (checked 640x480 vs 1600x1200)
  49. *
  50. * Constant derivation:
  51. *
  52. * Raw data:
  53. * Gain, GTOP, B, R, GBOT
  54. * 1.00, 0x105C, 0x1068, 0x10C8, 0x105C
  55. * 1.20, 0x106E, 0x107E, 0x10D6, 0x106E
  56. * 1.40, 0x10C0, 0x10CA, 0x10E5, 0x10C0
  57. * 1.60, 0x10C9, 0x10D4, 0x10F3, 0x10C9
  58. * 1.80, 0x10D2, 0x10DE, 0x11C1, 0x10D2
  59. * 2.00, 0x10DC, 0x10E9, 0x11C8, 0x10DC
  60. * 2.20, 0x10E5, 0x10F3, 0x11CF, 0x10E5
  61. * 2.40, 0x10EE, 0x10FE, 0x11D7, 0x10EE
  62. * 2.60, 0x10F7, 0x11C4, 0x11DE, 0x10F7
  63. * 2.80, 0x11C0, 0x11CA, 0x11E5, 0x11C0
  64. * 3.00, 0x11C5, 0x11CF, 0x11ED, 0x11C5
  65. *
  66. * zR = 0.0069605943152454778
  67. * about 3/431 = 0.0069605568445475635
  68. * zB = 0.0095695970695970703
  69. * about 6/627 = 0.0095693779904306216
  70. * zG = 0.010889328063241107
  71. * about 6/551 = 0.010889292196007259
  72. * about 10 bits for constant + 7 bits for value => at least 17 bit
  73. * intermediate with 32 bit ints should be fine for overflow etc
  74. * Essentially gains are in range 0-0x001FF
  75. *
  76. * However, V4L expects a main gain channel + R and B balance
  77. * To keep things simple for now saturate the values of balance is too high/low
  78. * This isn't really ideal but easy way to fit the Linux model
  79. *
  80. * Converted using gain model turns out to be quite linear:
  81. * Gain, GTOP, B, R, GBOT
  82. * 1.00, 92, 104, 144, 92
  83. * 1.20, 110, 126, 172, 110
  84. * 1.40, 128, 148, 202, 128
  85. * 1.60, 146, 168, 230, 146
  86. * 1.80, 164, 188, 260, 164
  87. * 2.00, 184, 210, 288, 184
  88. * 2.20, 202, 230, 316, 202
  89. * 2.40, 220, 252, 348, 220
  90. * 2.60, 238, 272, 376, 238
  91. * 2.80, 256, 296, 404, 256
  92. * 3.00, 276, 316, 436, 276
  93. *
  94. * Maximum gain is 0x7FF * 2 * 2 => 0x1FFC (8188)
  95. * or about 13 effective bits of gain
  96. * The highest the commercial driver goes in my setup 436
  97. * However, because could *maybe* damage circuits
  98. * limit the gain until have a reason to go higher
  99. * Solution: gain clipped and warning emitted
  100. */
  101. #define GAIN_MAX 511
  102. /* Frame sync is a short read */
  103. #define BULK_SIZE 0x4000
  104. /* MT9E001 reg names to give a rough approximation */
  105. #define REG_COARSE_INTEGRATION_TIME_ 0x3012
  106. #define REG_GROUPED_PARAMETER_HOLD_ 0x3022
  107. #define REG_MODE_SELECT 0x0100
  108. #define REG_OP_SYS_CLK_DIV 0x030A
  109. #define REG_VT_SYS_CLK_DIV 0x0302
  110. #define REG_PRE_PLL_CLK_DIV 0x0304
  111. #define REG_VT_PIX_CLK_DIV 0x0300
  112. #define REG_OP_PIX_CLK_DIV 0x0308
  113. #define REG_PLL_MULTIPLIER 0x0306
  114. #define REG_COARSE_INTEGRATION_TIME_ 0x3012
  115. #define REG_FRAME_LENGTH_LINES 0x0340
  116. #define REG_FRAME_LENGTH_LINES_ 0x300A
  117. #define REG_GREEN1_GAIN 0x3056
  118. #define REG_GREEN2_GAIN 0x305C
  119. #define REG_GROUPED_PARAMETER_HOLD 0x0104
  120. #define REG_LINE_LENGTH_PCK_ 0x300C
  121. #define REG_MODE_SELECT 0x0100
  122. #define REG_PLL_MULTIPLIER 0x0306
  123. #define REG_READ_MODE 0x3040
  124. #define REG_BLUE_GAIN 0x3058
  125. #define REG_RED_GAIN 0x305A
  126. #define REG_RESET_REGISTER 0x301A
  127. #define REG_SCALE_M 0x0404
  128. #define REG_SCALING_MODE 0x0400
  129. #define REG_SOFTWARE_RESET 0x0103
  130. #define REG_X_ADDR_END 0x0348
  131. #define REG_X_ADDR_START 0x0344
  132. #define REG_X_ADDR_START 0x0344
  133. #define REG_X_OUTPUT_SIZE 0x034C
  134. #define REG_Y_ADDR_END 0x034A
  135. #define REG_Y_ADDR_START 0x0346
  136. #define REG_Y_OUTPUT_SIZE 0x034E
  137. /* specific webcam descriptor */
  138. struct sd {
  139. struct gspca_dev gspca_dev; /* !! must be the first item */
  140. /* How many bytes this frame */
  141. unsigned int this_f;
  142. /*
  143. Device has separate gains for each Bayer quadrant
  144. V4L supports master gain which is referenced to G1/G2 and supplies
  145. individual balance controls for R/B
  146. */
  147. struct v4l2_ctrl *blue;
  148. struct v4l2_ctrl *red;
  149. };
  150. /* Used to simplify reg write error handling */
  151. struct cmd {
  152. u16 value;
  153. u16 index;
  154. };
  155. static const struct v4l2_pix_format vga_mode[] = {
  156. {800, 600,
  157. V4L2_PIX_FMT_SGRBG8,
  158. V4L2_FIELD_NONE,
  159. .bytesperline = 800,
  160. .sizeimage = 800 * 600,
  161. .colorspace = V4L2_COLORSPACE_SRGB},
  162. {1600, 1200,
  163. V4L2_PIX_FMT_SGRBG8,
  164. V4L2_FIELD_NONE,
  165. .bytesperline = 1600,
  166. .sizeimage = 1600 * 1200,
  167. .colorspace = V4L2_COLORSPACE_SRGB},
  168. {3264, 2448,
  169. V4L2_PIX_FMT_SGRBG8,
  170. V4L2_FIELD_NONE,
  171. .bytesperline = 3264,
  172. .sizeimage = 3264 * 2448,
  173. .colorspace = V4L2_COLORSPACE_SRGB},
  174. };
  175. /*
  176. * As theres no known frame sync, the only way to keep synced is to try hard
  177. * to never miss any packets
  178. */
  179. #if MAX_NURBS < 4
  180. #error "Not enough URBs in the gspca table"
  181. #endif
  182. static int val_reply(struct gspca_dev *gspca_dev, const char *reply, int rc)
  183. {
  184. if (rc < 0) {
  185. gspca_err(gspca_dev, "reply has error %d\n", rc);
  186. return -EIO;
  187. }
  188. if (rc != 1) {
  189. gspca_err(gspca_dev, "Bad reply size %d\n", rc);
  190. return -EIO;
  191. }
  192. if (reply[0] != 0x08) {
  193. gspca_err(gspca_dev, "Bad reply 0x%02x\n", (int)reply[0]);
  194. return -EIO;
  195. }
  196. return 0;
  197. }
  198. static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
  199. {
  200. char *buff = gspca_dev->usb_buf;
  201. int rc;
  202. gspca_dbg(gspca_dev, D_USBO,
  203. "reg_w bReq=0x0B, bReqT=0xC0, wVal=0x%04X, wInd=0x%04X\n\n",
  204. value, index);
  205. rc = usb_control_msg(gspca_dev->dev, usb_rcvctrlpipe(gspca_dev->dev, 0),
  206. 0x0B, 0xC0, value, index, buff, 1, 500);
  207. gspca_dbg(gspca_dev, D_USBO, "rc=%d, ret={0x%02x}\n", rc, (int)buff[0]);
  208. if (rc < 0) {
  209. gspca_err(gspca_dev, "Failed reg_w(0x0B, 0xC0, 0x%04X, 0x%04X) w/ rc %d\n",
  210. value, index, rc);
  211. gspca_dev->usb_err = rc;
  212. return;
  213. }
  214. if (val_reply(gspca_dev, buff, rc)) {
  215. gspca_err(gspca_dev, "Bad reply to reg_w(0x0B, 0xC0, 0x%04X, 0x%04X\n",
  216. value, index);
  217. gspca_dev->usb_err = -EIO;
  218. }
  219. }
  220. static void reg_w_buf(struct gspca_dev *gspca_dev,
  221. const struct cmd *p, int l)
  222. {
  223. do {
  224. reg_w(gspca_dev, p->value, p->index);
  225. p++;
  226. } while (--l > 0);
  227. }
  228. static void setexposure(struct gspca_dev *gspca_dev, s32 val)
  229. {
  230. u16 value;
  231. unsigned int w = gspca_dev->pixfmt.width;
  232. if (w == 800)
  233. value = val * 5;
  234. else if (w == 1600)
  235. value = val * 3;
  236. else if (w == 3264)
  237. value = val * 3 / 2;
  238. else {
  239. gspca_err(gspca_dev, "Invalid width %u\n", w);
  240. gspca_dev->usb_err = -EINVAL;
  241. return;
  242. }
  243. gspca_dbg(gspca_dev, D_STREAM, "exposure: 0x%04X ms\n\n", value);
  244. /* Wonder if theres a good reason for sending it twice */
  245. /* probably not but leave it in because...why not */
  246. reg_w(gspca_dev, value, REG_COARSE_INTEGRATION_TIME_);
  247. reg_w(gspca_dev, value, REG_COARSE_INTEGRATION_TIME_);
  248. }
  249. static int gainify(int in)
  250. {
  251. /*
  252. * TODO: check if there are any issues with corner cases
  253. * 0x000 (0):0x07F (127): regL
  254. * 0x080 (128) - 0x0FF (255): regM, regL
  255. * 0x100 (256) - max: regH, regM, regL
  256. */
  257. if (in <= 0x7F)
  258. return 0x1000 | in;
  259. else if (in <= 0xFF)
  260. return 0x1080 | in / 2;
  261. else
  262. return 0x1180 | in / 4;
  263. }
  264. static void setggain(struct gspca_dev *gspca_dev, u16 global_gain)
  265. {
  266. u16 normalized;
  267. normalized = gainify(global_gain);
  268. gspca_dbg(gspca_dev, D_STREAM, "gain G1/G2 (0x%04X): 0x%04X (src 0x%04X)\n\n",
  269. REG_GREEN1_GAIN,
  270. normalized, global_gain);
  271. reg_w(gspca_dev, normalized, REG_GREEN1_GAIN);
  272. reg_w(gspca_dev, normalized, REG_GREEN2_GAIN);
  273. }
  274. static void setbgain(struct gspca_dev *gspca_dev,
  275. u16 gain, u16 global_gain)
  276. {
  277. u16 normalized;
  278. normalized = global_gain +
  279. ((u32)global_gain) * gain / GAIN_MAX;
  280. if (normalized > GAIN_MAX) {
  281. gspca_dbg(gspca_dev, D_STREAM, "Truncating blue 0x%04X w/ value 0x%04X\n\n",
  282. GAIN_MAX, normalized);
  283. normalized = GAIN_MAX;
  284. }
  285. normalized = gainify(normalized);
  286. gspca_dbg(gspca_dev, D_STREAM, "gain B (0x%04X): 0x%04X w/ source 0x%04X\n\n",
  287. REG_BLUE_GAIN, normalized, gain);
  288. reg_w(gspca_dev, normalized, REG_BLUE_GAIN);
  289. }
  290. static void setrgain(struct gspca_dev *gspca_dev,
  291. u16 gain, u16 global_gain)
  292. {
  293. u16 normalized;
  294. normalized = global_gain +
  295. ((u32)global_gain) * gain / GAIN_MAX;
  296. if (normalized > GAIN_MAX) {
  297. gspca_dbg(gspca_dev, D_STREAM, "Truncating gain 0x%04X w/ value 0x%04X\n\n",
  298. GAIN_MAX, normalized);
  299. normalized = GAIN_MAX;
  300. }
  301. normalized = gainify(normalized);
  302. gspca_dbg(gspca_dev, D_STREAM, "gain R (0x%04X): 0x%04X w / source 0x%04X\n\n",
  303. REG_RED_GAIN, normalized, gain);
  304. reg_w(gspca_dev, normalized, REG_RED_GAIN);
  305. }
  306. static void configure_wh(struct gspca_dev *gspca_dev)
  307. {
  308. unsigned int w = gspca_dev->pixfmt.width;
  309. gspca_dbg(gspca_dev, D_STREAM, "configure_wh\n\n");
  310. if (w == 800) {
  311. static const struct cmd reg_init_res[] = {
  312. {0x0060, REG_X_ADDR_START},
  313. {0x0CD9, REG_X_ADDR_END},
  314. {0x0036, REG_Y_ADDR_START},
  315. {0x098F, REG_Y_ADDR_END},
  316. {0x07C7, REG_READ_MODE},
  317. };
  318. reg_w_buf(gspca_dev,
  319. reg_init_res, ARRAY_SIZE(reg_init_res));
  320. } else if (w == 1600) {
  321. static const struct cmd reg_init_res[] = {
  322. {0x009C, REG_X_ADDR_START},
  323. {0x0D19, REG_X_ADDR_END},
  324. {0x0068, REG_Y_ADDR_START},
  325. {0x09C5, REG_Y_ADDR_END},
  326. {0x06C3, REG_READ_MODE},
  327. };
  328. reg_w_buf(gspca_dev,
  329. reg_init_res, ARRAY_SIZE(reg_init_res));
  330. } else if (w == 3264) {
  331. static const struct cmd reg_init_res[] = {
  332. {0x00E8, REG_X_ADDR_START},
  333. {0x0DA7, REG_X_ADDR_END},
  334. {0x009E, REG_Y_ADDR_START},
  335. {0x0A2D, REG_Y_ADDR_END},
  336. {0x0241, REG_READ_MODE},
  337. };
  338. reg_w_buf(gspca_dev,
  339. reg_init_res, ARRAY_SIZE(reg_init_res));
  340. } else {
  341. gspca_err(gspca_dev, "bad width %u\n", w);
  342. gspca_dev->usb_err = -EINVAL;
  343. return;
  344. }
  345. reg_w(gspca_dev, 0x0000, REG_SCALING_MODE);
  346. reg_w(gspca_dev, 0x0010, REG_SCALE_M);
  347. reg_w(gspca_dev, w, REG_X_OUTPUT_SIZE);
  348. reg_w(gspca_dev, gspca_dev->pixfmt.height, REG_Y_OUTPUT_SIZE);
  349. if (w == 800) {
  350. reg_w(gspca_dev, 0x0384, REG_FRAME_LENGTH_LINES_);
  351. reg_w(gspca_dev, 0x0960, REG_LINE_LENGTH_PCK_);
  352. } else if (w == 1600) {
  353. reg_w(gspca_dev, 0x0640, REG_FRAME_LENGTH_LINES_);
  354. reg_w(gspca_dev, 0x0FA0, REG_LINE_LENGTH_PCK_);
  355. } else if (w == 3264) {
  356. reg_w(gspca_dev, 0x0B4B, REG_FRAME_LENGTH_LINES_);
  357. reg_w(gspca_dev, 0x1F40, REG_LINE_LENGTH_PCK_);
  358. } else {
  359. gspca_err(gspca_dev, "bad width %u\n", w);
  360. gspca_dev->usb_err = -EINVAL;
  361. return;
  362. }
  363. }
  364. /* Packets that were encrypted, no idea if the grouping is significant */
  365. static void configure_encrypted(struct gspca_dev *gspca_dev)
  366. {
  367. static const struct cmd reg_init_begin[] = {
  368. {0x0100, REG_SOFTWARE_RESET},
  369. {0x0000, REG_MODE_SELECT},
  370. {0x0100, REG_GROUPED_PARAMETER_HOLD},
  371. {0x0004, REG_VT_PIX_CLK_DIV},
  372. {0x0001, REG_VT_SYS_CLK_DIV},
  373. {0x0008, REG_OP_PIX_CLK_DIV},
  374. {0x0001, REG_OP_SYS_CLK_DIV},
  375. {0x0004, REG_PRE_PLL_CLK_DIV},
  376. {0x0040, REG_PLL_MULTIPLIER},
  377. {0x0000, REG_GROUPED_PARAMETER_HOLD},
  378. {0x0100, REG_GROUPED_PARAMETER_HOLD},
  379. };
  380. static const struct cmd reg_init_end[] = {
  381. {0x0000, REG_GROUPED_PARAMETER_HOLD},
  382. {0x0301, 0x31AE},
  383. {0x0805, 0x3064},
  384. {0x0071, 0x3170},
  385. {0x10DE, REG_RESET_REGISTER},
  386. {0x0000, REG_MODE_SELECT},
  387. {0x0010, REG_PLL_MULTIPLIER},
  388. {0x0100, REG_MODE_SELECT},
  389. };
  390. gspca_dbg(gspca_dev, D_STREAM, "Encrypted begin, w = %u\n\n",
  391. gspca_dev->pixfmt.width);
  392. reg_w_buf(gspca_dev, reg_init_begin, ARRAY_SIZE(reg_init_begin));
  393. configure_wh(gspca_dev);
  394. reg_w_buf(gspca_dev, reg_init_end, ARRAY_SIZE(reg_init_end));
  395. reg_w(gspca_dev, 0x0100, REG_GROUPED_PARAMETER_HOLD);
  396. reg_w(gspca_dev, 0x0000, REG_GROUPED_PARAMETER_HOLD);
  397. gspca_dbg(gspca_dev, D_STREAM, "Encrypted end\n\n");
  398. }
  399. static int configure(struct gspca_dev *gspca_dev)
  400. {
  401. int rc;
  402. char *buff = gspca_dev->usb_buf;
  403. gspca_dbg(gspca_dev, D_STREAM, "configure()\n\n");
  404. /*
  405. * First driver sets a sort of encryption key
  406. * A number of futur requests of this type have wValue and wIndex
  407. * encrypted as follows:
  408. * -Compute key = this wValue rotate left by 4 bits
  409. * (decrypt.py rotates right because we are decrypting)
  410. * -Later packets encrypt packets by XOR'ing with key
  411. * XOR encrypt/decrypt is symmetrical
  412. * wValue, and wIndex are encrypted
  413. * bRequest is not and bRequestType is always 0xC0
  414. * This allows resyncing if key is unknown?
  415. * By setting 0 we XOR with 0 and the shifting and XOR drops out
  416. */
  417. rc = usb_control_msg(gspca_dev->dev, usb_rcvctrlpipe(gspca_dev->dev, 0),
  418. 0x16, 0xC0, 0x0000, 0x0000, buff, 2, 500);
  419. if (val_reply(gspca_dev, buff, rc)) {
  420. gspca_err(gspca_dev, "failed key req\n");
  421. return -EIO;
  422. }
  423. /*
  424. * Next does some sort of 2 packet challenge / response
  425. * evidence suggests its an Atmel I2C crypto part but nobody cares to
  426. * look
  427. * (to make sure its not cloned hardware?)
  428. * Ignore: I want to work with their hardware, not clone it
  429. * 16 bytes out challenge, requestType: 0x40
  430. * 16 bytes in response, requestType: 0xC0
  431. */
  432. rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
  433. 0x01, 0x40, 0x0001, 0x000F, NULL, 0, 500);
  434. if (rc < 0) {
  435. gspca_err(gspca_dev, "failed to replay packet 176 w/ rc %d\n",
  436. rc);
  437. return rc;
  438. }
  439. rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
  440. 0x01, 0x40, 0x0000, 0x000F, NULL, 0, 500);
  441. if (rc < 0) {
  442. gspca_err(gspca_dev, "failed to replay packet 178 w/ rc %d\n",
  443. rc);
  444. return rc;
  445. }
  446. rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
  447. 0x01, 0x40, 0x0001, 0x000F, NULL, 0, 500);
  448. if (rc < 0) {
  449. gspca_err(gspca_dev, "failed to replay packet 180 w/ rc %d\n",
  450. rc);
  451. return rc;
  452. }
  453. /*
  454. * Serial number? Doesn't seem to be required
  455. * cam1: \xE6\x0D\x00\x00, cam2: \x70\x19\x00\x00
  456. * rc = usb_control_msg(gspca_dev->dev,
  457. * usb_rcvctrlpipe(gspca_dev->dev, 0),
  458. * 0x20, 0xC0, 0x0000, 0x0000, buff, 4, 500);
  459. */
  460. /* Large (EEPROM?) read, skip it since no idea what to do with it */
  461. gspca_dev->usb_err = 0;
  462. configure_encrypted(gspca_dev);
  463. if (gspca_dev->usb_err)
  464. return gspca_dev->usb_err;
  465. /* Omitted this by accident, does not work without it */
  466. rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
  467. 0x01, 0x40, 0x0003, 0x000F, NULL, 0, 500);
  468. if (rc < 0) {
  469. gspca_err(gspca_dev, "failed to replay final packet w/ rc %d\n",
  470. rc);
  471. return rc;
  472. }
  473. gspca_dbg(gspca_dev, D_STREAM, "Configure complete\n\n");
  474. return 0;
  475. }
  476. static int sd_config(struct gspca_dev *gspca_dev,
  477. const struct usb_device_id *id)
  478. {
  479. gspca_dev->cam.cam_mode = vga_mode;
  480. gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
  481. /* Yes we want URBs and we want them now! */
  482. gspca_dev->cam.no_urb_create = 0;
  483. gspca_dev->cam.bulk_nurbs = 4;
  484. /* Largest size the windows driver uses */
  485. gspca_dev->cam.bulk_size = BULK_SIZE;
  486. /* Def need to use bulk transfers */
  487. gspca_dev->cam.bulk = 1;
  488. return 0;
  489. }
  490. static int sd_start(struct gspca_dev *gspca_dev)
  491. {
  492. struct sd *sd = (struct sd *) gspca_dev;
  493. int rc;
  494. sd->this_f = 0;
  495. rc = configure(gspca_dev);
  496. if (rc < 0) {
  497. gspca_err(gspca_dev, "Failed configure\n");
  498. return rc;
  499. }
  500. /* First two frames have messed up gains
  501. Drop them to avoid special cases in user apps? */
  502. return 0;
  503. }
  504. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  505. u8 *data, /* isoc packet */
  506. int len) /* iso packet length */
  507. {
  508. struct sd *sd = (struct sd *) gspca_dev;
  509. if (len != BULK_SIZE) {
  510. /* can we finish a frame? */
  511. if (sd->this_f + len == gspca_dev->pixfmt.sizeimage) {
  512. gspca_frame_add(gspca_dev, LAST_PACKET, data, len);
  513. gspca_dbg(gspca_dev, D_FRAM, "finish frame sz %u/%u w/ len %u\n\n",
  514. sd->this_f, gspca_dev->pixfmt.sizeimage, len);
  515. /* lost some data, discard the frame */
  516. } else {
  517. gspca_frame_add(gspca_dev, DISCARD_PACKET, NULL, 0);
  518. gspca_dbg(gspca_dev, D_FRAM, "abort frame sz %u/%u w/ len %u\n\n",
  519. sd->this_f, gspca_dev->pixfmt.sizeimage, len);
  520. }
  521. sd->this_f = 0;
  522. } else {
  523. if (sd->this_f == 0)
  524. gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
  525. else
  526. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  527. sd->this_f += len;
  528. }
  529. }
  530. static int sd_init(struct gspca_dev *gspca_dev)
  531. {
  532. return 0;
  533. }
  534. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  535. {
  536. struct gspca_dev *gspca_dev =
  537. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  538. struct sd *sd = (struct sd *) gspca_dev;
  539. gspca_dev->usb_err = 0;
  540. if (!gspca_dev->streaming)
  541. return 0;
  542. switch (ctrl->id) {
  543. case V4L2_CID_EXPOSURE:
  544. setexposure(gspca_dev, ctrl->val);
  545. break;
  546. case V4L2_CID_GAIN:
  547. /* gspca_dev->gain automatically updated */
  548. setggain(gspca_dev, gspca_dev->gain->val);
  549. break;
  550. case V4L2_CID_BLUE_BALANCE:
  551. sd->blue->val = ctrl->val;
  552. setbgain(gspca_dev, sd->blue->val, gspca_dev->gain->val);
  553. break;
  554. case V4L2_CID_RED_BALANCE:
  555. sd->red->val = ctrl->val;
  556. setrgain(gspca_dev, sd->red->val, gspca_dev->gain->val);
  557. break;
  558. }
  559. return gspca_dev->usb_err;
  560. }
  561. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  562. .s_ctrl = sd_s_ctrl,
  563. };
  564. static int sd_init_controls(struct gspca_dev *gspca_dev)
  565. {
  566. struct sd *sd = (struct sd *) gspca_dev;
  567. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  568. gspca_dev->vdev.ctrl_handler = hdl;
  569. v4l2_ctrl_handler_init(hdl, 4);
  570. gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  571. /* Mostly limited by URB timeouts */
  572. /* XXX: make dynamic based on frame rate? */
  573. V4L2_CID_EXPOSURE, 0, 800, 1, 350);
  574. gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  575. V4L2_CID_GAIN, 0, 511, 1, 128);
  576. sd->blue = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  577. V4L2_CID_BLUE_BALANCE, 0, 1023, 1, 80);
  578. sd->red = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  579. V4L2_CID_RED_BALANCE, 0, 1023, 1, 295);
  580. if (hdl->error) {
  581. gspca_err(gspca_dev, "Could not initialize controls\n");
  582. return hdl->error;
  583. }
  584. return 0;
  585. }
  586. /* sub-driver description */
  587. static const struct sd_desc sd_desc = {
  588. .name = MODULE_NAME,
  589. .config = sd_config,
  590. .init = sd_init,
  591. .init_controls = sd_init_controls,
  592. .start = sd_start,
  593. .pkt_scan = sd_pkt_scan,
  594. };
  595. /* Table of supported USB devices */
  596. static const struct usb_device_id device_table[] = {
  597. /* Commented out devices should be related */
  598. /* AS: AmScope, TT: ToupTek */
  599. /* { USB_DEVICE(0x0547, 0x6035) }, TT UCMOS00350KPA */
  600. /* { USB_DEVICE(0x0547, 0x6130) }, TT UCMOS01300KPA */
  601. /* { USB_DEVICE(0x0547, 0x6200) }, TT UCMOS02000KPA */
  602. /* { USB_DEVICE(0x0547, 0x6310) }, TT UCMOS03100KPA */
  603. /* { USB_DEVICE(0x0547, 0x6510) }, TT UCMOS05100KPA */
  604. /* { USB_DEVICE(0x0547, 0x6800) }, TT UCMOS08000KPA */
  605. /* { USB_DEVICE(0x0547, 0x6801) }, TT UCMOS08000KPB */
  606. { USB_DEVICE(0x0547, 0x6801) }, /* TT UCMOS08000KPB, AS MU800 */
  607. /* { USB_DEVICE(0x0547, 0x6900) }, TT UCMOS09000KPA */
  608. /* { USB_DEVICE(0x0547, 0x6901) }, TT UCMOS09000KPB */
  609. /* { USB_DEVICE(0x0547, 0x6010) }, TT UCMOS10000KPA */
  610. /* { USB_DEVICE(0x0547, 0x6014) }, TT UCMOS14000KPA */
  611. /* { USB_DEVICE(0x0547, 0x6131) }, TT UCMOS01300KMA */
  612. /* { USB_DEVICE(0x0547, 0x6511) }, TT UCMOS05100KMA */
  613. /* { USB_DEVICE(0x0547, 0x8080) }, TT UHCCD00800KPA */
  614. /* { USB_DEVICE(0x0547, 0x8140) }, TT UHCCD01400KPA */
  615. /* { USB_DEVICE(0x0547, 0x8141) }, TT EXCCD01400KPA */
  616. /* { USB_DEVICE(0x0547, 0x8200) }, TT UHCCD02000KPA */
  617. /* { USB_DEVICE(0x0547, 0x8201) }, TT UHCCD02000KPB */
  618. /* { USB_DEVICE(0x0547, 0x8310) }, TT UHCCD03100KPA */
  619. /* { USB_DEVICE(0x0547, 0x8500) }, TT UHCCD05000KPA */
  620. /* { USB_DEVICE(0x0547, 0x8510) }, TT UHCCD05100KPA */
  621. /* { USB_DEVICE(0x0547, 0x8600) }, TT UHCCD06000KPA */
  622. /* { USB_DEVICE(0x0547, 0x8800) }, TT UHCCD08000KPA */
  623. /* { USB_DEVICE(0x0547, 0x8315) }, TT UHCCD03150KPA */
  624. /* { USB_DEVICE(0x0547, 0x7800) }, TT UHCCD00800KMA */
  625. /* { USB_DEVICE(0x0547, 0x7140) }, TT UHCCD01400KMA */
  626. /* { USB_DEVICE(0x0547, 0x7141) }, TT UHCCD01400KMB */
  627. /* { USB_DEVICE(0x0547, 0x7200) }, TT UHCCD02000KMA */
  628. /* { USB_DEVICE(0x0547, 0x7315) }, TT UHCCD03150KMA */
  629. { }
  630. };
  631. MODULE_DEVICE_TABLE(usb, device_table);
  632. static int sd_probe(struct usb_interface *intf,
  633. const struct usb_device_id *id)
  634. {
  635. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  636. THIS_MODULE);
  637. }
  638. static struct usb_driver sd_driver = {
  639. .name = MODULE_NAME,
  640. .id_table = device_table,
  641. .probe = sd_probe,
  642. .disconnect = gspca_disconnect,
  643. #ifdef CONFIG_PM
  644. .suspend = gspca_suspend,
  645. .resume = gspca_resume,
  646. #endif
  647. };
  648. static int __init sd_mod_init(void)
  649. {
  650. int ret;
  651. ret = usb_register(&sd_driver);
  652. if (ret < 0)
  653. return ret;
  654. return 0;
  655. }
  656. static void __exit sd_mod_exit(void)
  657. {
  658. usb_deregister(&sd_driver);
  659. }
  660. module_init(sd_mod_init);
  661. module_exit(sd_mod_exit);