stk1135.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * Syntek STK1135 subdriver
  3. *
  4. * Copyright (c) 2013 Ondrej Zary
  5. *
  6. * Based on Syntekdriver (stk11xx) by Nicolas VIVIEN:
  7. * http://syntekdriver.sourceforge.net
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #define MODULE_NAME "stk1135"
  25. #include "gspca.h"
  26. #include "stk1135.h"
  27. MODULE_AUTHOR("Ondrej Zary");
  28. MODULE_DESCRIPTION("Syntek STK1135 USB Camera Driver");
  29. MODULE_LICENSE("GPL");
  30. /* specific webcam descriptor */
  31. struct sd {
  32. struct gspca_dev gspca_dev; /* !! must be the first item */
  33. u8 pkt_seq;
  34. u8 sensor_page;
  35. bool flip_status;
  36. u8 flip_debounce;
  37. struct v4l2_ctrl *hflip;
  38. struct v4l2_ctrl *vflip;
  39. };
  40. static const struct v4l2_pix_format stk1135_modes[] = {
  41. /* default mode (this driver supports variable resolution) */
  42. {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  43. .bytesperline = 640,
  44. .sizeimage = 640 * 480,
  45. .colorspace = V4L2_COLORSPACE_SRGB},
  46. };
  47. /* -- read a register -- */
  48. static u8 reg_r(struct gspca_dev *gspca_dev, u16 index)
  49. {
  50. struct usb_device *dev = gspca_dev->dev;
  51. int ret;
  52. if (gspca_dev->usb_err < 0)
  53. return 0;
  54. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  55. 0x00,
  56. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  57. 0x00,
  58. index,
  59. gspca_dev->usb_buf, 1,
  60. 500);
  61. PDEBUG(D_USBI, "reg_r 0x%x=0x%02x", index, gspca_dev->usb_buf[0]);
  62. if (ret < 0) {
  63. pr_err("reg_r 0x%x err %d\n", index, ret);
  64. gspca_dev->usb_err = ret;
  65. return 0;
  66. }
  67. return gspca_dev->usb_buf[0];
  68. }
  69. /* -- write a register -- */
  70. static void reg_w(struct gspca_dev *gspca_dev, u16 index, u8 val)
  71. {
  72. int ret;
  73. struct usb_device *dev = gspca_dev->dev;
  74. if (gspca_dev->usb_err < 0)
  75. return;
  76. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  77. 0x01,
  78. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  79. val,
  80. index,
  81. NULL,
  82. 0,
  83. 500);
  84. PDEBUG(D_USBO, "reg_w 0x%x:=0x%02x", index, val);
  85. if (ret < 0) {
  86. pr_err("reg_w 0x%x err %d\n", index, ret);
  87. gspca_dev->usb_err = ret;
  88. }
  89. }
  90. static void reg_w_mask(struct gspca_dev *gspca_dev, u16 index, u8 val, u8 mask)
  91. {
  92. val = (reg_r(gspca_dev, index) & ~mask) | (val & mask);
  93. reg_w(gspca_dev, index, val);
  94. }
  95. /* this function is called at probe time */
  96. static int sd_config(struct gspca_dev *gspca_dev,
  97. const struct usb_device_id *id)
  98. {
  99. gspca_dev->cam.cam_mode = stk1135_modes;
  100. gspca_dev->cam.nmodes = ARRAY_SIZE(stk1135_modes);
  101. return 0;
  102. }
  103. static int stk1135_serial_wait_ready(struct gspca_dev *gspca_dev)
  104. {
  105. int i = 0;
  106. u8 val;
  107. do {
  108. val = reg_r(gspca_dev, STK1135_REG_SICTL + 1);
  109. if (i++ > 500) { /* maximum retry count */
  110. pr_err("serial bus timeout: status=0x%02x\n", val);
  111. return -1;
  112. }
  113. /* repeat if BUSY or WRITE/READ not finished */
  114. } while ((val & 0x10) || !(val & 0x05));
  115. return 0;
  116. }
  117. static u8 sensor_read_8(struct gspca_dev *gspca_dev, u8 addr)
  118. {
  119. reg_w(gspca_dev, STK1135_REG_SBUSR, addr);
  120. /* begin read */
  121. reg_w(gspca_dev, STK1135_REG_SICTL, 0x20);
  122. /* wait until finished */
  123. if (stk1135_serial_wait_ready(gspca_dev)) {
  124. pr_err("Sensor read failed\n");
  125. return 0;
  126. }
  127. return reg_r(gspca_dev, STK1135_REG_SBUSR + 1);
  128. }
  129. static u16 sensor_read_16(struct gspca_dev *gspca_dev, u8 addr)
  130. {
  131. return (sensor_read_8(gspca_dev, addr) << 8) |
  132. sensor_read_8(gspca_dev, 0xf1);
  133. }
  134. static void sensor_write_8(struct gspca_dev *gspca_dev, u8 addr, u8 data)
  135. {
  136. /* load address and data registers */
  137. reg_w(gspca_dev, STK1135_REG_SBUSW, addr);
  138. reg_w(gspca_dev, STK1135_REG_SBUSW + 1, data);
  139. /* begin write */
  140. reg_w(gspca_dev, STK1135_REG_SICTL, 0x01);
  141. /* wait until finished */
  142. if (stk1135_serial_wait_ready(gspca_dev)) {
  143. pr_err("Sensor write failed\n");
  144. return;
  145. }
  146. }
  147. static void sensor_write_16(struct gspca_dev *gspca_dev, u8 addr, u16 data)
  148. {
  149. sensor_write_8(gspca_dev, addr, data >> 8);
  150. sensor_write_8(gspca_dev, 0xf1, data & 0xff);
  151. }
  152. static void sensor_set_page(struct gspca_dev *gspca_dev, u8 page)
  153. {
  154. struct sd *sd = (struct sd *) gspca_dev;
  155. if (page != sd->sensor_page) {
  156. sensor_write_16(gspca_dev, 0xf0, page);
  157. sd->sensor_page = page;
  158. }
  159. }
  160. static u16 sensor_read(struct gspca_dev *gspca_dev, u16 reg)
  161. {
  162. sensor_set_page(gspca_dev, reg >> 8);
  163. return sensor_read_16(gspca_dev, reg & 0xff);
  164. }
  165. static void sensor_write(struct gspca_dev *gspca_dev, u16 reg, u16 val)
  166. {
  167. sensor_set_page(gspca_dev, reg >> 8);
  168. sensor_write_16(gspca_dev, reg & 0xff, val);
  169. }
  170. static void sensor_write_mask(struct gspca_dev *gspca_dev,
  171. u16 reg, u16 val, u16 mask)
  172. {
  173. val = (sensor_read(gspca_dev, reg) & ~mask) | (val & mask);
  174. sensor_write(gspca_dev, reg, val);
  175. }
  176. struct sensor_val {
  177. u16 reg;
  178. u16 val;
  179. };
  180. /* configure MT9M112 sensor */
  181. static void stk1135_configure_mt9m112(struct gspca_dev *gspca_dev)
  182. {
  183. static const struct sensor_val cfg[] = {
  184. /* restart&reset, chip enable, reserved */
  185. { 0x00d, 0x000b }, { 0x00d, 0x0008 }, { 0x035, 0x0022 },
  186. /* mode ctl: AWB on, AE both, clip aper corr, defect corr, AE */
  187. { 0x106, 0x700e },
  188. { 0x2dd, 0x18e0 }, /* B-R thresholds, */
  189. /* AWB */
  190. { 0x21f, 0x0180 }, /* Cb and Cr limits */
  191. { 0x220, 0xc814 }, { 0x221, 0x8080 }, /* lum limits, RGB gain */
  192. { 0x222, 0xa078 }, { 0x223, 0xa078 }, /* R, B limit */
  193. { 0x224, 0x5f20 }, { 0x228, 0xea02 }, /* mtx adj lim, adv ctl */
  194. { 0x229, 0x867a }, /* wide gates */
  195. /* Color correction */
  196. /* imager gains base, delta, delta signs */
  197. { 0x25e, 0x594c }, { 0x25f, 0x4d51 }, { 0x260, 0x0002 },
  198. /* AWB adv ctl 2, gain offs */
  199. { 0x2ef, 0x0008 }, { 0x2f2, 0x0000 },
  200. /* base matrix signs, scale K1-5, K6-9 */
  201. { 0x202, 0x00ee }, { 0x203, 0x3923 }, { 0x204, 0x0724 },
  202. /* base matrix coef */
  203. { 0x209, 0x00cd }, { 0x20a, 0x0093 }, { 0x20b, 0x0004 },/*K1-3*/
  204. { 0x20c, 0x005c }, { 0x20d, 0x00d9 }, { 0x20e, 0x0053 },/*K4-6*/
  205. { 0x20f, 0x0008 }, { 0x210, 0x0091 }, { 0x211, 0x00cf },/*K7-9*/
  206. { 0x215, 0x0000 }, /* delta mtx signs */
  207. /* delta matrix coef */
  208. { 0x216, 0x0000 }, { 0x217, 0x0000 }, { 0x218, 0x0000 },/*D1-3*/
  209. { 0x219, 0x0000 }, { 0x21a, 0x0000 }, { 0x21b, 0x0000 },/*D4-6*/
  210. { 0x21c, 0x0000 }, { 0x21d, 0x0000 }, { 0x21e, 0x0000 },/*D7-9*/
  211. /* enable & disable manual WB to apply color corr. settings */
  212. { 0x106, 0xf00e }, { 0x106, 0x700e },
  213. /* Lens shading correction */
  214. { 0x180, 0x0007 }, /* control */
  215. /* vertical knee 0, 2+1, 4+3 */
  216. { 0x181, 0xde13 }, { 0x182, 0xebe2 }, { 0x183, 0x00f6 }, /* R */
  217. { 0x184, 0xe114 }, { 0x185, 0xeadd }, { 0x186, 0xfdf6 }, /* G */
  218. { 0x187, 0xe511 }, { 0x188, 0xede6 }, { 0x189, 0xfbf7 }, /* B */
  219. /* horizontal knee 0, 2+1, 4+3, 5 */
  220. { 0x18a, 0xd613 }, { 0x18b, 0xedec }, /* R .. */
  221. { 0x18c, 0xf9f2 }, { 0x18d, 0x0000 }, /* .. R */
  222. { 0x18e, 0xd815 }, { 0x18f, 0xe9ea }, /* G .. */
  223. { 0x190, 0xf9f1 }, { 0x191, 0x0002 }, /* .. G */
  224. { 0x192, 0xde10 }, { 0x193, 0xefef }, /* B .. */
  225. { 0x194, 0xfbf4 }, { 0x195, 0x0002 }, /* .. B */
  226. /* vertical knee 6+5, 8+7 */
  227. { 0x1b6, 0x0e06 }, { 0x1b7, 0x2713 }, /* R */
  228. { 0x1b8, 0x1106 }, { 0x1b9, 0x2713 }, /* G */
  229. { 0x1ba, 0x0c03 }, { 0x1bb, 0x2a0f }, /* B */
  230. /* horizontal knee 7+6, 9+8, 10 */
  231. { 0x1bc, 0x1208 }, { 0x1bd, 0x1a16 }, { 0x1be, 0x0022 }, /* R */
  232. { 0x1bf, 0x150a }, { 0x1c0, 0x1c1a }, { 0x1c1, 0x002d }, /* G */
  233. { 0x1c2, 0x1109 }, { 0x1c3, 0x1414 }, { 0x1c4, 0x002a }, /* B */
  234. { 0x106, 0x740e }, /* enable lens shading correction */
  235. /* Gamma correction - context A */
  236. { 0x153, 0x0b03 }, { 0x154, 0x4722 }, { 0x155, 0xac82 },
  237. { 0x156, 0xdac7 }, { 0x157, 0xf5e9 }, { 0x158, 0xff00 },
  238. /* Gamma correction - context B */
  239. { 0x1dc, 0x0b03 }, { 0x1dd, 0x4722 }, { 0x1de, 0xac82 },
  240. { 0x1df, 0xdac7 }, { 0x1e0, 0xf5e9 }, { 0x1e1, 0xff00 },
  241. /* output format: RGB, invert output pixclock, output bayer */
  242. { 0x13a, 0x4300 }, { 0x19b, 0x4300 }, /* for context A, B */
  243. { 0x108, 0x0180 }, /* format control - enable bayer row flip */
  244. { 0x22f, 0xd100 }, { 0x29c, 0xd100 }, /* AE A, B */
  245. /* default prg conf, prg ctl - by 0x2d2, prg advance - PA1 */
  246. { 0x2d2, 0x0000 }, { 0x2cc, 0x0004 }, { 0x2cb, 0x0001 },
  247. { 0x22e, 0x0c3c }, { 0x267, 0x1010 }, /* AE tgt ctl, gain lim */
  248. /* PLL */
  249. { 0x065, 0xa000 }, /* clk ctl - enable PLL (clear bit 14) */
  250. { 0x066, 0x2003 }, { 0x067, 0x0501 }, /* PLL M=128, N=3, P=1 */
  251. { 0x065, 0x2000 }, /* disable PLL bypass (clear bit 15) */
  252. { 0x005, 0x01b8 }, { 0x007, 0x00d8 }, /* horiz blanking B, A */
  253. /* AE line size, shutter delay limit */
  254. { 0x239, 0x06c0 }, { 0x23b, 0x040e }, /* for context A */
  255. { 0x23a, 0x06c0 }, { 0x23c, 0x0564 }, /* for context B */
  256. /* shutter width basis 60Hz, 50Hz */
  257. { 0x257, 0x0208 }, { 0x258, 0x0271 }, /* for context A */
  258. { 0x259, 0x0209 }, { 0x25a, 0x0271 }, /* for context B */
  259. { 0x25c, 0x120d }, { 0x25d, 0x1712 }, /* flicker 60Hz, 50Hz */
  260. { 0x264, 0x5e1c }, /* reserved */
  261. /* flicker, AE gain limits, gain zone limits */
  262. { 0x25b, 0x0003 }, { 0x236, 0x7810 }, { 0x237, 0x8304 },
  263. { 0x008, 0x0021 }, /* vert blanking A */
  264. };
  265. int i;
  266. u16 width, height;
  267. for (i = 0; i < ARRAY_SIZE(cfg); i++)
  268. sensor_write(gspca_dev, cfg[i].reg, cfg[i].val);
  269. /* set output size */
  270. width = gspca_dev->pixfmt.width;
  271. height = gspca_dev->pixfmt.height;
  272. if (width <= 640 && height <= 512) { /* context A (half readout speed)*/
  273. sensor_write(gspca_dev, 0x1a7, width);
  274. sensor_write(gspca_dev, 0x1aa, height);
  275. /* set read mode context A */
  276. sensor_write(gspca_dev, 0x0c8, 0x0000);
  277. /* set resize, read mode, vblank, hblank context A */
  278. sensor_write(gspca_dev, 0x2c8, 0x0000);
  279. } else { /* context B (full readout speed) */
  280. sensor_write(gspca_dev, 0x1a1, width);
  281. sensor_write(gspca_dev, 0x1a4, height);
  282. /* set read mode context B */
  283. sensor_write(gspca_dev, 0x0c8, 0x0008);
  284. /* set resize, read mode, vblank, hblank context B */
  285. sensor_write(gspca_dev, 0x2c8, 0x040b);
  286. }
  287. }
  288. static void stk1135_configure_clock(struct gspca_dev *gspca_dev)
  289. {
  290. /* configure SCLKOUT */
  291. reg_w(gspca_dev, STK1135_REG_TMGEN, 0x12);
  292. /* set 1 clock per pixel */
  293. /* and positive edge clocked pulse high when pixel counter = 0 */
  294. reg_w(gspca_dev, STK1135_REG_TCP1 + 0, 0x41);
  295. reg_w(gspca_dev, STK1135_REG_TCP1 + 1, 0x00);
  296. reg_w(gspca_dev, STK1135_REG_TCP1 + 2, 0x00);
  297. reg_w(gspca_dev, STK1135_REG_TCP1 + 3, 0x00);
  298. /* enable CLKOUT for sensor */
  299. reg_w(gspca_dev, STK1135_REG_SENSO + 0, 0x10);
  300. /* disable STOP clock */
  301. reg_w(gspca_dev, STK1135_REG_SENSO + 1, 0x00);
  302. /* set lower 8 bits of PLL feedback divider */
  303. reg_w(gspca_dev, STK1135_REG_SENSO + 3, 0x07);
  304. /* set other PLL parameters */
  305. reg_w(gspca_dev, STK1135_REG_PLLFD, 0x06);
  306. /* enable timing generator */
  307. reg_w(gspca_dev, STK1135_REG_TMGEN, 0x80);
  308. /* enable PLL */
  309. reg_w(gspca_dev, STK1135_REG_SENSO + 2, 0x04);
  310. /* set serial interface clock divider (30MHz/0x1f*16+2) = 60240 kHz) */
  311. reg_w(gspca_dev, STK1135_REG_SICTL + 2, 0x1f);
  312. /* wait a while for sensor to catch up */
  313. udelay(1000);
  314. }
  315. static void stk1135_camera_disable(struct gspca_dev *gspca_dev)
  316. {
  317. /* set capture end Y position to 0 */
  318. reg_w(gspca_dev, STK1135_REG_CIEPO + 2, 0x00);
  319. reg_w(gspca_dev, STK1135_REG_CIEPO + 3, 0x00);
  320. /* disable capture */
  321. reg_w_mask(gspca_dev, STK1135_REG_SCTRL, 0x00, 0x80);
  322. /* enable sensor standby and diasble chip enable */
  323. sensor_write_mask(gspca_dev, 0x00d, 0x0004, 0x000c);
  324. /* disable PLL */
  325. reg_w_mask(gspca_dev, STK1135_REG_SENSO + 2, 0x00, 0x01);
  326. /* disable timing generator */
  327. reg_w(gspca_dev, STK1135_REG_TMGEN, 0x00);
  328. /* enable STOP clock */
  329. reg_w(gspca_dev, STK1135_REG_SENSO + 1, 0x20);
  330. /* disable CLKOUT for sensor */
  331. reg_w(gspca_dev, STK1135_REG_SENSO, 0x00);
  332. /* disable sensor (GPIO5) and enable GPIO0,3,6 (?) - sensor standby? */
  333. reg_w(gspca_dev, STK1135_REG_GCTRL, 0x49);
  334. }
  335. /* this function is called at probe and resume time */
  336. static int sd_init(struct gspca_dev *gspca_dev)
  337. {
  338. u16 sensor_id;
  339. char *sensor_name;
  340. struct sd *sd = (struct sd *) gspca_dev;
  341. /* set GPIO3,4,5,6 direction to output */
  342. reg_w(gspca_dev, STK1135_REG_GCTRL + 2, 0x78);
  343. /* enable sensor (GPIO5) */
  344. reg_w(gspca_dev, STK1135_REG_GCTRL, (1 << 5));
  345. /* disable ROM interface */
  346. reg_w(gspca_dev, STK1135_REG_GCTRL + 3, 0x80);
  347. /* enable interrupts from GPIO8 (flip sensor) and GPIO9 (???) */
  348. reg_w(gspca_dev, STK1135_REG_ICTRL + 1, 0x00);
  349. reg_w(gspca_dev, STK1135_REG_ICTRL + 3, 0x03);
  350. /* enable remote wakeup from GPIO9 (???) */
  351. reg_w(gspca_dev, STK1135_REG_RMCTL + 1, 0x00);
  352. reg_w(gspca_dev, STK1135_REG_RMCTL + 3, 0x02);
  353. /* reset serial interface */
  354. reg_w(gspca_dev, STK1135_REG_SICTL, 0x80);
  355. reg_w(gspca_dev, STK1135_REG_SICTL, 0x00);
  356. /* set sensor address */
  357. reg_w(gspca_dev, STK1135_REG_SICTL + 3, 0xba);
  358. /* disable alt 2-wire serial interface */
  359. reg_w(gspca_dev, STK1135_REG_ASIC + 3, 0x00);
  360. stk1135_configure_clock(gspca_dev);
  361. /* read sensor ID */
  362. sd->sensor_page = 0xff;
  363. sensor_id = sensor_read(gspca_dev, 0x000);
  364. switch (sensor_id) {
  365. case 0x148c:
  366. sensor_name = "MT9M112";
  367. break;
  368. default:
  369. sensor_name = "unknown";
  370. }
  371. pr_info("Detected sensor type %s (0x%x)\n", sensor_name, sensor_id);
  372. stk1135_camera_disable(gspca_dev);
  373. return gspca_dev->usb_err;
  374. }
  375. /* -- start the camera -- */
  376. static int sd_start(struct gspca_dev *gspca_dev)
  377. {
  378. struct sd *sd = (struct sd *) gspca_dev;
  379. u16 width, height;
  380. /* enable sensor (GPIO5) */
  381. reg_w(gspca_dev, STK1135_REG_GCTRL, (1 << 5));
  382. stk1135_configure_clock(gspca_dev);
  383. /* set capture start position X = 0, Y = 0 */
  384. reg_w(gspca_dev, STK1135_REG_CISPO + 0, 0x00);
  385. reg_w(gspca_dev, STK1135_REG_CISPO + 1, 0x00);
  386. reg_w(gspca_dev, STK1135_REG_CISPO + 2, 0x00);
  387. reg_w(gspca_dev, STK1135_REG_CISPO + 3, 0x00);
  388. /* set capture end position */
  389. width = gspca_dev->pixfmt.width;
  390. height = gspca_dev->pixfmt.height;
  391. reg_w(gspca_dev, STK1135_REG_CIEPO + 0, width & 0xff);
  392. reg_w(gspca_dev, STK1135_REG_CIEPO + 1, width >> 8);
  393. reg_w(gspca_dev, STK1135_REG_CIEPO + 2, height & 0xff);
  394. reg_w(gspca_dev, STK1135_REG_CIEPO + 3, height >> 8);
  395. /* set 8-bit mode */
  396. reg_w(gspca_dev, STK1135_REG_SCTRL, 0x20);
  397. stk1135_configure_mt9m112(gspca_dev);
  398. /* enable capture */
  399. reg_w_mask(gspca_dev, STK1135_REG_SCTRL, 0x80, 0x80);
  400. if (gspca_dev->usb_err >= 0)
  401. PDEBUG(D_STREAM, "camera started alt: 0x%02x",
  402. gspca_dev->alt);
  403. sd->pkt_seq = 0;
  404. return gspca_dev->usb_err;
  405. }
  406. static void sd_stopN(struct gspca_dev *gspca_dev)
  407. {
  408. struct usb_device *dev = gspca_dev->dev;
  409. usb_set_interface(dev, gspca_dev->iface, 0);
  410. stk1135_camera_disable(gspca_dev);
  411. PDEBUG(D_STREAM, "camera stopped");
  412. }
  413. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  414. u8 *data, /* isoc packet */
  415. int len) /* iso packet length */
  416. {
  417. struct sd *sd = (struct sd *) gspca_dev;
  418. int skip = sizeof(struct stk1135_pkt_header);
  419. bool flip;
  420. enum gspca_packet_type pkt_type = INTER_PACKET;
  421. struct stk1135_pkt_header *hdr = (void *)data;
  422. u8 seq;
  423. if (len < 4) {
  424. PDEBUG(D_PACK, "received short packet (less than 4 bytes)");
  425. return;
  426. }
  427. /* GPIO 8 is flip sensor (1 = normal position, 0 = flipped to back) */
  428. flip = !(le16_to_cpu(hdr->gpio) & (1 << 8));
  429. /* it's a switch, needs software debounce */
  430. if (sd->flip_status != flip)
  431. sd->flip_debounce++;
  432. else
  433. sd->flip_debounce = 0;
  434. /* check sequence number (not present in new frame packets) */
  435. if (!(hdr->flags & STK1135_HDR_FRAME_START)) {
  436. seq = hdr->seq & STK1135_HDR_SEQ_MASK;
  437. if (seq != sd->pkt_seq) {
  438. PDEBUG(D_PACK, "received out-of-sequence packet");
  439. /* resync sequence and discard packet */
  440. sd->pkt_seq = seq;
  441. gspca_dev->last_packet_type = DISCARD_PACKET;
  442. return;
  443. }
  444. }
  445. sd->pkt_seq++;
  446. if (sd->pkt_seq > STK1135_HDR_SEQ_MASK)
  447. sd->pkt_seq = 0;
  448. if (len == sizeof(struct stk1135_pkt_header))
  449. return;
  450. if (hdr->flags & STK1135_HDR_FRAME_START) { /* new frame */
  451. skip = 8; /* the header is longer */
  452. gspca_frame_add(gspca_dev, LAST_PACKET, data, 0);
  453. pkt_type = FIRST_PACKET;
  454. }
  455. gspca_frame_add(gspca_dev, pkt_type, data + skip, len - skip);
  456. }
  457. static void sethflip(struct gspca_dev *gspca_dev, s32 val)
  458. {
  459. struct sd *sd = (struct sd *) gspca_dev;
  460. if (sd->flip_status)
  461. val = !val;
  462. sensor_write_mask(gspca_dev, 0x020, val ? 0x0002 : 0x0000 , 0x0002);
  463. }
  464. static void setvflip(struct gspca_dev *gspca_dev, s32 val)
  465. {
  466. struct sd *sd = (struct sd *) gspca_dev;
  467. if (sd->flip_status)
  468. val = !val;
  469. sensor_write_mask(gspca_dev, 0x020, val ? 0x0001 : 0x0000 , 0x0001);
  470. }
  471. static void stk1135_dq_callback(struct gspca_dev *gspca_dev)
  472. {
  473. struct sd *sd = (struct sd *) gspca_dev;
  474. if (sd->flip_debounce > 100) {
  475. sd->flip_status = !sd->flip_status;
  476. sethflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip));
  477. setvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->vflip));
  478. }
  479. }
  480. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  481. {
  482. struct gspca_dev *gspca_dev =
  483. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  484. gspca_dev->usb_err = 0;
  485. if (!gspca_dev->streaming)
  486. return 0;
  487. switch (ctrl->id) {
  488. case V4L2_CID_HFLIP:
  489. sethflip(gspca_dev, ctrl->val);
  490. break;
  491. case V4L2_CID_VFLIP:
  492. setvflip(gspca_dev, ctrl->val);
  493. break;
  494. }
  495. return gspca_dev->usb_err;
  496. }
  497. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  498. .s_ctrl = sd_s_ctrl,
  499. };
  500. static int sd_init_controls(struct gspca_dev *gspca_dev)
  501. {
  502. struct sd *sd = (struct sd *) gspca_dev;
  503. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  504. gspca_dev->vdev.ctrl_handler = hdl;
  505. v4l2_ctrl_handler_init(hdl, 2);
  506. sd->hflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  507. V4L2_CID_HFLIP, 0, 1, 1, 0);
  508. sd->vflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  509. V4L2_CID_VFLIP, 0, 1, 1, 0);
  510. if (hdl->error) {
  511. pr_err("Could not initialize controls\n");
  512. return hdl->error;
  513. }
  514. return 0;
  515. }
  516. static void stk1135_try_fmt(struct gspca_dev *gspca_dev, struct v4l2_format *fmt)
  517. {
  518. fmt->fmt.pix.width = clamp(fmt->fmt.pix.width, 32U, 1280U);
  519. fmt->fmt.pix.height = clamp(fmt->fmt.pix.height, 32U, 1024U);
  520. /* round up to even numbers */
  521. fmt->fmt.pix.width += (fmt->fmt.pix.width & 1);
  522. fmt->fmt.pix.height += (fmt->fmt.pix.height & 1);
  523. fmt->fmt.pix.bytesperline = fmt->fmt.pix.width;
  524. fmt->fmt.pix.sizeimage = fmt->fmt.pix.width * fmt->fmt.pix.height;
  525. }
  526. static int stk1135_enum_framesizes(struct gspca_dev *gspca_dev,
  527. struct v4l2_frmsizeenum *fsize)
  528. {
  529. if (fsize->index != 0 || fsize->pixel_format != V4L2_PIX_FMT_SBGGR8)
  530. return -EINVAL;
  531. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  532. fsize->stepwise.min_width = 32;
  533. fsize->stepwise.min_height = 32;
  534. fsize->stepwise.max_width = 1280;
  535. fsize->stepwise.max_height = 1024;
  536. fsize->stepwise.step_width = 2;
  537. fsize->stepwise.step_height = 2;
  538. return 0;
  539. }
  540. /* sub-driver description */
  541. static const struct sd_desc sd_desc = {
  542. .name = MODULE_NAME,
  543. .config = sd_config,
  544. .init = sd_init,
  545. .init_controls = sd_init_controls,
  546. .start = sd_start,
  547. .stopN = sd_stopN,
  548. .pkt_scan = sd_pkt_scan,
  549. .dq_callback = stk1135_dq_callback,
  550. .try_fmt = stk1135_try_fmt,
  551. .enum_framesizes = stk1135_enum_framesizes,
  552. };
  553. /* -- module initialisation -- */
  554. static const struct usb_device_id device_table[] = {
  555. {USB_DEVICE(0x174f, 0x6a31)}, /* ASUS laptop, MT9M112 sensor */
  556. {}
  557. };
  558. MODULE_DEVICE_TABLE(usb, device_table);
  559. /* -- device connect -- */
  560. static int sd_probe(struct usb_interface *intf,
  561. const struct usb_device_id *id)
  562. {
  563. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  564. THIS_MODULE);
  565. }
  566. static struct usb_driver sd_driver = {
  567. .name = MODULE_NAME,
  568. .id_table = device_table,
  569. .probe = sd_probe,
  570. .disconnect = gspca_disconnect,
  571. #ifdef CONFIG_PM
  572. .suspend = gspca_suspend,
  573. .resume = gspca_resume,
  574. .reset_resume = gspca_resume,
  575. #endif
  576. };
  577. module_usb_driver(sd_driver);