touptek.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. PERR("reply has error %d", rc);
  186. return -EIO;
  187. }
  188. if (rc != 1) {
  189. PERR("Bad reply size %d", rc);
  190. return -EIO;
  191. }
  192. if (reply[0] != 0x08) {
  193. PERR("Bad reply 0x%02X", 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[1];
  201. int rc;
  202. PDEBUG(D_USBO,
  203. "reg_w bReq=0x0B, bReqT=0xC0, wVal=0x%04X, wInd=0x%04X\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. PDEBUG(D_USBO, "rc=%d, ret={0x%02X}", rc, buff[0]);
  208. if (rc < 0) {
  209. PERR("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. PERR("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. PERR("Invalid width %u\n", w);
  240. gspca_dev->usb_err = -EINVAL;
  241. return;
  242. }
  243. PDEBUG(D_STREAM, "exposure: 0x%04X ms\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. PDEBUG(D_STREAM, "gain G1/G2 (0x%04X): 0x%04X (src 0x%04X)\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. PDEBUG(D_STREAM, "Truncating blue 0x%04X w/ value 0x%04X\n",
  282. GAIN_MAX, normalized);
  283. normalized = GAIN_MAX;
  284. }
  285. normalized = gainify(normalized);
  286. PDEBUG(D_STREAM, "gain B (0x%04X): 0x%04X w/ source 0x%04X\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. PDEBUG(D_STREAM, "Truncating gain 0x%04X w/ value 0x%04X\n",
  298. GAIN_MAX, normalized);
  299. normalized = GAIN_MAX;
  300. }
  301. normalized = gainify(normalized);
  302. PDEBUG(D_STREAM, "gain R (0x%04X): 0x%04X w / source 0x%04X\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. PDEBUG(D_STREAM, "configure_wh\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. PERR("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. PERR("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. PDEBUG(D_STREAM, "Encrypted begin, w = %u\n", gspca_dev->pixfmt.width);
  391. reg_w_buf(gspca_dev, reg_init_begin, ARRAY_SIZE(reg_init_begin));
  392. configure_wh(gspca_dev);
  393. reg_w_buf(gspca_dev, reg_init_end, ARRAY_SIZE(reg_init_end));
  394. reg_w(gspca_dev, 0x0100, REG_GROUPED_PARAMETER_HOLD);
  395. reg_w(gspca_dev, 0x0000, REG_GROUPED_PARAMETER_HOLD);
  396. PDEBUG(D_STREAM, "Encrypted end\n");
  397. }
  398. static int configure(struct gspca_dev *gspca_dev)
  399. {
  400. int rc;
  401. uint8_t buff[4];
  402. PDEBUG(D_STREAM, "configure()\n");
  403. /*
  404. * First driver sets a sort of encryption key
  405. * A number of futur requests of this type have wValue and wIndex
  406. * encrypted as follows:
  407. * -Compute key = this wValue rotate left by 4 bits
  408. * (decrypt.py rotates right because we are decrypting)
  409. * -Later packets encrypt packets by XOR'ing with key
  410. * XOR encrypt/decrypt is symmetrical
  411. * wValue, and wIndex are encrypted
  412. * bRequest is not and bRequestType is always 0xC0
  413. * This allows resyncing if key is unknown?
  414. * By setting 0 we XOR with 0 and the shifting and XOR drops out
  415. */
  416. rc = usb_control_msg(gspca_dev->dev, usb_rcvctrlpipe(gspca_dev->dev, 0),
  417. 0x16, 0xC0, 0x0000, 0x0000, buff, 2, 500);
  418. if (val_reply(gspca_dev, buff, rc)) {
  419. PERR("failed key req");
  420. return -EIO;
  421. }
  422. /*
  423. * Next does some sort of 2 packet challenge / response
  424. * evidence suggests its an Atmel I2C crypto part but nobody cares to
  425. * look
  426. * (to make sure its not cloned hardware?)
  427. * Ignore: I want to work with their hardware, not clone it
  428. * 16 bytes out challenge, requestType: 0x40
  429. * 16 bytes in response, requestType: 0xC0
  430. */
  431. rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
  432. 0x01, 0x40, 0x0001, 0x000F, NULL, 0, 500);
  433. if (rc < 0) {
  434. PERR("failed to replay packet 176 w/ rc %d\n", rc);
  435. return rc;
  436. }
  437. rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
  438. 0x01, 0x40, 0x0000, 0x000F, NULL, 0, 500);
  439. if (rc < 0) {
  440. PERR("failed to replay packet 178 w/ rc %d\n", rc);
  441. return rc;
  442. }
  443. rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
  444. 0x01, 0x40, 0x0001, 0x000F, NULL, 0, 500);
  445. if (rc < 0) {
  446. PERR("failed to replay packet 180 w/ rc %d\n", rc);
  447. return rc;
  448. }
  449. /*
  450. * Serial number? Doesn't seem to be required
  451. * cam1: \xE6\x0D\x00\x00, cam2: \x70\x19\x00\x00
  452. * rc = usb_control_msg(gspca_dev->dev,
  453. * usb_rcvctrlpipe(gspca_dev->dev, 0),
  454. * 0x20, 0xC0, 0x0000, 0x0000, buff, 4, 500);
  455. */
  456. /* Large (EEPROM?) read, skip it since no idea what to do with it */
  457. gspca_dev->usb_err = 0;
  458. configure_encrypted(gspca_dev);
  459. if (gspca_dev->usb_err)
  460. return gspca_dev->usb_err;
  461. /* Omitted this by accident, does not work without it */
  462. rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
  463. 0x01, 0x40, 0x0003, 0x000F, NULL, 0, 500);
  464. if (rc < 0) {
  465. PERR("failed to replay final packet w/ rc %d\n", rc);
  466. return rc;
  467. }
  468. PDEBUG(D_STREAM, "Configure complete\n");
  469. return 0;
  470. }
  471. static int sd_config(struct gspca_dev *gspca_dev,
  472. const struct usb_device_id *id)
  473. {
  474. gspca_dev->cam.cam_mode = vga_mode;
  475. gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
  476. /* Yes we want URBs and we want them now! */
  477. gspca_dev->cam.no_urb_create = 0;
  478. gspca_dev->cam.bulk_nurbs = 4;
  479. /* Largest size the windows driver uses */
  480. gspca_dev->cam.bulk_size = BULK_SIZE;
  481. /* Def need to use bulk transfers */
  482. gspca_dev->cam.bulk = 1;
  483. return 0;
  484. }
  485. static int sd_start(struct gspca_dev *gspca_dev)
  486. {
  487. struct sd *sd = (struct sd *) gspca_dev;
  488. int rc;
  489. sd->this_f = 0;
  490. rc = configure(gspca_dev);
  491. if (rc < 0) {
  492. PERR("Failed configure");
  493. return rc;
  494. }
  495. /* First two frames have messed up gains
  496. Drop them to avoid special cases in user apps? */
  497. return 0;
  498. }
  499. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  500. u8 *data, /* isoc packet */
  501. int len) /* iso packet length */
  502. {
  503. struct sd *sd = (struct sd *) gspca_dev;
  504. if (len != BULK_SIZE) {
  505. /* can we finish a frame? */
  506. if (sd->this_f + len == gspca_dev->pixfmt.sizeimage) {
  507. gspca_frame_add(gspca_dev, LAST_PACKET, data, len);
  508. PDEBUG(D_FRAM, "finish frame sz %u/%u w/ len %u\n",
  509. sd->this_f, gspca_dev->pixfmt.sizeimage, len);
  510. /* lost some data, discard the frame */
  511. } else {
  512. gspca_frame_add(gspca_dev, DISCARD_PACKET, NULL, 0);
  513. PDEBUG(D_FRAM, "abort frame sz %u/%u w/ len %u\n",
  514. sd->this_f, gspca_dev->pixfmt.sizeimage, len);
  515. }
  516. sd->this_f = 0;
  517. } else {
  518. if (sd->this_f == 0)
  519. gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
  520. else
  521. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  522. sd->this_f += len;
  523. }
  524. }
  525. static int sd_init(struct gspca_dev *gspca_dev)
  526. {
  527. return 0;
  528. }
  529. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  530. {
  531. struct gspca_dev *gspca_dev =
  532. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  533. struct sd *sd = (struct sd *) gspca_dev;
  534. gspca_dev->usb_err = 0;
  535. if (!gspca_dev->streaming)
  536. return 0;
  537. switch (ctrl->id) {
  538. case V4L2_CID_EXPOSURE:
  539. setexposure(gspca_dev, ctrl->val);
  540. break;
  541. case V4L2_CID_GAIN:
  542. /* gspca_dev->gain automatically updated */
  543. setggain(gspca_dev, gspca_dev->gain->val);
  544. break;
  545. case V4L2_CID_BLUE_BALANCE:
  546. sd->blue->val = ctrl->val;
  547. setbgain(gspca_dev, sd->blue->val, gspca_dev->gain->val);
  548. break;
  549. case V4L2_CID_RED_BALANCE:
  550. sd->red->val = ctrl->val;
  551. setrgain(gspca_dev, sd->red->val, gspca_dev->gain->val);
  552. break;
  553. }
  554. return gspca_dev->usb_err;
  555. }
  556. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  557. .s_ctrl = sd_s_ctrl,
  558. };
  559. static int sd_init_controls(struct gspca_dev *gspca_dev)
  560. {
  561. struct sd *sd = (struct sd *) gspca_dev;
  562. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  563. gspca_dev->vdev.ctrl_handler = hdl;
  564. v4l2_ctrl_handler_init(hdl, 4);
  565. gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  566. /* Mostly limited by URB timeouts */
  567. /* XXX: make dynamic based on frame rate? */
  568. V4L2_CID_EXPOSURE, 0, 800, 1, 350);
  569. gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  570. V4L2_CID_GAIN, 0, 511, 1, 128);
  571. sd->blue = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  572. V4L2_CID_BLUE_BALANCE, 0, 1023, 1, 80);
  573. sd->red = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  574. V4L2_CID_RED_BALANCE, 0, 1023, 1, 295);
  575. if (hdl->error) {
  576. PERR("Could not initialize controls\n");
  577. return hdl->error;
  578. }
  579. return 0;
  580. }
  581. /* sub-driver description */
  582. static const struct sd_desc sd_desc = {
  583. .name = MODULE_NAME,
  584. .config = sd_config,
  585. .init = sd_init,
  586. .init_controls = sd_init_controls,
  587. .start = sd_start,
  588. .pkt_scan = sd_pkt_scan,
  589. };
  590. /* Table of supported USB devices */
  591. static const struct usb_device_id device_table[] = {
  592. /* Commented out devices should be related */
  593. /* AS: AmScope, TT: ToupTek */
  594. /* { USB_DEVICE(0x0547, 0x6035) }, TT UCMOS00350KPA */
  595. /* { USB_DEVICE(0x0547, 0x6130) }, TT UCMOS01300KPA */
  596. /* { USB_DEVICE(0x0547, 0x6200) }, TT UCMOS02000KPA */
  597. /* { USB_DEVICE(0x0547, 0x6310) }, TT UCMOS03100KPA */
  598. /* { USB_DEVICE(0x0547, 0x6510) }, TT UCMOS05100KPA */
  599. /* { USB_DEVICE(0x0547, 0x6800) }, TT UCMOS08000KPA */
  600. /* { USB_DEVICE(0x0547, 0x6801) }, TT UCMOS08000KPB */
  601. { USB_DEVICE(0x0547, 0x6801) }, /* TT UCMOS08000KPB, AS MU800 */
  602. /* { USB_DEVICE(0x0547, 0x6900) }, TT UCMOS09000KPA */
  603. /* { USB_DEVICE(0x0547, 0x6901) }, TT UCMOS09000KPB */
  604. /* { USB_DEVICE(0x0547, 0x6010) }, TT UCMOS10000KPA */
  605. /* { USB_DEVICE(0x0547, 0x6014) }, TT UCMOS14000KPA */
  606. /* { USB_DEVICE(0x0547, 0x6131) }, TT UCMOS01300KMA */
  607. /* { USB_DEVICE(0x0547, 0x6511) }, TT UCMOS05100KMA */
  608. /* { USB_DEVICE(0x0547, 0x8080) }, TT UHCCD00800KPA */
  609. /* { USB_DEVICE(0x0547, 0x8140) }, TT UHCCD01400KPA */
  610. /* { USB_DEVICE(0x0547, 0x8141) }, TT EXCCD01400KPA */
  611. /* { USB_DEVICE(0x0547, 0x8200) }, TT UHCCD02000KPA */
  612. /* { USB_DEVICE(0x0547, 0x8201) }, TT UHCCD02000KPB */
  613. /* { USB_DEVICE(0x0547, 0x8310) }, TT UHCCD03100KPA */
  614. /* { USB_DEVICE(0x0547, 0x8500) }, TT UHCCD05000KPA */
  615. /* { USB_DEVICE(0x0547, 0x8510) }, TT UHCCD05100KPA */
  616. /* { USB_DEVICE(0x0547, 0x8600) }, TT UHCCD06000KPA */
  617. /* { USB_DEVICE(0x0547, 0x8800) }, TT UHCCD08000KPA */
  618. /* { USB_DEVICE(0x0547, 0x8315) }, TT UHCCD03150KPA */
  619. /* { USB_DEVICE(0x0547, 0x7800) }, TT UHCCD00800KMA */
  620. /* { USB_DEVICE(0x0547, 0x7140) }, TT UHCCD01400KMA */
  621. /* { USB_DEVICE(0x0547, 0x7141) }, TT UHCCD01400KMB */
  622. /* { USB_DEVICE(0x0547, 0x7200) }, TT UHCCD02000KMA */
  623. /* { USB_DEVICE(0x0547, 0x7315) }, TT UHCCD03150KMA */
  624. { }
  625. };
  626. MODULE_DEVICE_TABLE(usb, device_table);
  627. static int sd_probe(struct usb_interface *intf,
  628. const struct usb_device_id *id)
  629. {
  630. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  631. THIS_MODULE);
  632. }
  633. static struct usb_driver sd_driver = {
  634. .name = MODULE_NAME,
  635. .id_table = device_table,
  636. .probe = sd_probe,
  637. .disconnect = gspca_disconnect,
  638. #ifdef CONFIG_PM
  639. .suspend = gspca_suspend,
  640. .resume = gspca_resume,
  641. #endif
  642. };
  643. static int __init sd_mod_init(void)
  644. {
  645. int ret;
  646. ret = usb_register(&sd_driver);
  647. if (ret < 0)
  648. return ret;
  649. return 0;
  650. }
  651. static void __exit sd_mod_exit(void)
  652. {
  653. usb_deregister(&sd_driver);
  654. }
  655. module_init(sd_mod_init);
  656. module_exit(sd_mod_exit);