stk014.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Syntek DV4000 (STK014) subdriver
  3. *
  4. * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #define MODULE_NAME "stk014"
  18. #include "gspca.h"
  19. #include "jpeg.h"
  20. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  21. MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver");
  22. MODULE_LICENSE("GPL");
  23. #define QUALITY 50
  24. /* specific webcam descriptor */
  25. struct sd {
  26. struct gspca_dev gspca_dev; /* !! must be the first item */
  27. u8 jpeg_hdr[JPEG_HDR_SZ];
  28. };
  29. static const struct v4l2_pix_format vga_mode[] = {
  30. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  31. .bytesperline = 320,
  32. .sizeimage = 320 * 240 * 3 / 8 + 590,
  33. .colorspace = V4L2_COLORSPACE_JPEG,
  34. .priv = 1},
  35. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  36. .bytesperline = 640,
  37. .sizeimage = 640 * 480 * 3 / 8 + 590,
  38. .colorspace = V4L2_COLORSPACE_JPEG,
  39. .priv = 0},
  40. };
  41. /* -- read a register -- */
  42. static u8 reg_r(struct gspca_dev *gspca_dev,
  43. __u16 index)
  44. {
  45. struct usb_device *dev = gspca_dev->dev;
  46. int ret;
  47. if (gspca_dev->usb_err < 0)
  48. return 0;
  49. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  50. 0x00,
  51. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  52. 0x00,
  53. index,
  54. gspca_dev->usb_buf, 1,
  55. 500);
  56. if (ret < 0) {
  57. pr_err("reg_r err %d\n", ret);
  58. gspca_dev->usb_err = ret;
  59. return 0;
  60. }
  61. return gspca_dev->usb_buf[0];
  62. }
  63. /* -- write a register -- */
  64. static void reg_w(struct gspca_dev *gspca_dev,
  65. __u16 index, __u16 value)
  66. {
  67. struct usb_device *dev = gspca_dev->dev;
  68. int ret;
  69. if (gspca_dev->usb_err < 0)
  70. return;
  71. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  72. 0x01,
  73. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  74. value,
  75. index,
  76. NULL,
  77. 0,
  78. 500);
  79. if (ret < 0) {
  80. pr_err("reg_w err %d\n", ret);
  81. gspca_dev->usb_err = ret;
  82. }
  83. }
  84. /* -- get a bulk value (4 bytes) -- */
  85. static void rcv_val(struct gspca_dev *gspca_dev,
  86. int ads)
  87. {
  88. struct usb_device *dev = gspca_dev->dev;
  89. int alen, ret;
  90. reg_w(gspca_dev, 0x634, (ads >> 16) & 0xff);
  91. reg_w(gspca_dev, 0x635, (ads >> 8) & 0xff);
  92. reg_w(gspca_dev, 0x636, ads & 0xff);
  93. reg_w(gspca_dev, 0x637, 0);
  94. reg_w(gspca_dev, 0x638, 4); /* len & 0xff */
  95. reg_w(gspca_dev, 0x639, 0); /* len >> 8 */
  96. reg_w(gspca_dev, 0x63a, 0);
  97. reg_w(gspca_dev, 0x63b, 0);
  98. reg_w(gspca_dev, 0x630, 5);
  99. if (gspca_dev->usb_err < 0)
  100. return;
  101. ret = usb_bulk_msg(dev,
  102. usb_rcvbulkpipe(dev, 0x05),
  103. gspca_dev->usb_buf,
  104. 4, /* length */
  105. &alen,
  106. 500); /* timeout in milliseconds */
  107. if (ret < 0) {
  108. pr_err("rcv_val err %d\n", ret);
  109. gspca_dev->usb_err = ret;
  110. }
  111. }
  112. /* -- send a bulk value -- */
  113. static void snd_val(struct gspca_dev *gspca_dev,
  114. int ads,
  115. unsigned int val)
  116. {
  117. struct usb_device *dev = gspca_dev->dev;
  118. int alen, ret;
  119. __u8 seq = 0;
  120. if (ads == 0x003f08) {
  121. reg_r(gspca_dev, 0x0704);
  122. seq = reg_r(gspca_dev, 0x0705);
  123. reg_r(gspca_dev, 0x0650);
  124. reg_w(gspca_dev, 0x654, seq);
  125. } else {
  126. reg_w(gspca_dev, 0x654, (ads >> 16) & 0xff);
  127. }
  128. reg_w(gspca_dev, 0x655, (ads >> 8) & 0xff);
  129. reg_w(gspca_dev, 0x656, ads & 0xff);
  130. reg_w(gspca_dev, 0x657, 0);
  131. reg_w(gspca_dev, 0x658, 0x04); /* size */
  132. reg_w(gspca_dev, 0x659, 0);
  133. reg_w(gspca_dev, 0x65a, 0);
  134. reg_w(gspca_dev, 0x65b, 0);
  135. reg_w(gspca_dev, 0x650, 5);
  136. if (gspca_dev->usb_err < 0)
  137. return;
  138. gspca_dev->usb_buf[0] = val >> 24;
  139. gspca_dev->usb_buf[1] = val >> 16;
  140. gspca_dev->usb_buf[2] = val >> 8;
  141. gspca_dev->usb_buf[3] = val;
  142. ret = usb_bulk_msg(dev,
  143. usb_sndbulkpipe(dev, 6),
  144. gspca_dev->usb_buf,
  145. 4,
  146. &alen,
  147. 500); /* timeout in milliseconds */
  148. if (ret < 0) {
  149. pr_err("snd_val err %d\n", ret);
  150. gspca_dev->usb_err = ret;
  151. } else {
  152. if (ads == 0x003f08) {
  153. seq += 4;
  154. seq &= 0x3f;
  155. reg_w(gspca_dev, 0x705, seq);
  156. }
  157. }
  158. }
  159. /* set a camera parameter */
  160. static void set_par(struct gspca_dev *gspca_dev,
  161. int parval)
  162. {
  163. snd_val(gspca_dev, 0x003f08, parval);
  164. }
  165. static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
  166. {
  167. int parval;
  168. parval = 0x06000000 /* whiteness */
  169. + (val << 16);
  170. set_par(gspca_dev, parval);
  171. }
  172. static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
  173. {
  174. int parval;
  175. parval = 0x07000000 /* contrast */
  176. + (val << 16);
  177. set_par(gspca_dev, parval);
  178. }
  179. static void setcolors(struct gspca_dev *gspca_dev, s32 val)
  180. {
  181. int parval;
  182. parval = 0x08000000 /* saturation */
  183. + (val << 16);
  184. set_par(gspca_dev, parval);
  185. }
  186. static void setlightfreq(struct gspca_dev *gspca_dev, s32 val)
  187. {
  188. set_par(gspca_dev, val == 1
  189. ? 0x33640000 /* 50 Hz */
  190. : 0x33780000); /* 60 Hz */
  191. }
  192. /* this function is called at probe time */
  193. static int sd_config(struct gspca_dev *gspca_dev,
  194. const struct usb_device_id *id)
  195. {
  196. gspca_dev->cam.cam_mode = vga_mode;
  197. gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
  198. return 0;
  199. }
  200. /* this function is called at probe and resume time */
  201. static int sd_init(struct gspca_dev *gspca_dev)
  202. {
  203. u8 ret;
  204. /* check if the device responds */
  205. usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
  206. ret = reg_r(gspca_dev, 0x0740);
  207. if (gspca_dev->usb_err >= 0) {
  208. if (ret != 0xff) {
  209. pr_err("init reg: 0x%02x\n", ret);
  210. gspca_dev->usb_err = -EIO;
  211. }
  212. }
  213. return gspca_dev->usb_err;
  214. }
  215. /* -- start the camera -- */
  216. static int sd_start(struct gspca_dev *gspca_dev)
  217. {
  218. struct sd *sd = (struct sd *) gspca_dev;
  219. int ret, value;
  220. /* create the JPEG header */
  221. jpeg_define(sd->jpeg_hdr, gspca_dev->pixfmt.height,
  222. gspca_dev->pixfmt.width,
  223. 0x22); /* JPEG 411 */
  224. jpeg_set_qual(sd->jpeg_hdr, QUALITY);
  225. /* work on alternate 1 */
  226. usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
  227. set_par(gspca_dev, 0x10000000);
  228. set_par(gspca_dev, 0x00000000);
  229. set_par(gspca_dev, 0x8002e001);
  230. set_par(gspca_dev, 0x14000000);
  231. if (gspca_dev->pixfmt.width > 320)
  232. value = 0x8002e001; /* 640x480 */
  233. else
  234. value = 0x4001f000; /* 320x240 */
  235. set_par(gspca_dev, value);
  236. ret = usb_set_interface(gspca_dev->dev,
  237. gspca_dev->iface,
  238. gspca_dev->alt);
  239. if (ret < 0) {
  240. pr_err("set intf %d %d failed\n",
  241. gspca_dev->iface, gspca_dev->alt);
  242. gspca_dev->usb_err = ret;
  243. goto out;
  244. }
  245. reg_r(gspca_dev, 0x0630);
  246. rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
  247. reg_r(gspca_dev, 0x0650);
  248. snd_val(gspca_dev, 0x000020, 0xffffffff);
  249. reg_w(gspca_dev, 0x0620, 0);
  250. reg_w(gspca_dev, 0x0630, 0);
  251. reg_w(gspca_dev, 0x0640, 0);
  252. reg_w(gspca_dev, 0x0650, 0);
  253. reg_w(gspca_dev, 0x0660, 0);
  254. set_par(gspca_dev, 0x09800000); /* Red ? */
  255. set_par(gspca_dev, 0x0a800000); /* Green ? */
  256. set_par(gspca_dev, 0x0b800000); /* Blue ? */
  257. set_par(gspca_dev, 0x0d030000); /* Gamma ? */
  258. /* start the video flow */
  259. set_par(gspca_dev, 0x01000000);
  260. set_par(gspca_dev, 0x01000000);
  261. if (gspca_dev->usb_err >= 0)
  262. gspca_dbg(gspca_dev, D_STREAM, "camera started alt: 0x%02x\n",
  263. gspca_dev->alt);
  264. out:
  265. return gspca_dev->usb_err;
  266. }
  267. static void sd_stopN(struct gspca_dev *gspca_dev)
  268. {
  269. struct usb_device *dev = gspca_dev->dev;
  270. set_par(gspca_dev, 0x02000000);
  271. set_par(gspca_dev, 0x02000000);
  272. usb_set_interface(dev, gspca_dev->iface, 1);
  273. reg_r(gspca_dev, 0x0630);
  274. rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
  275. reg_r(gspca_dev, 0x0650);
  276. snd_val(gspca_dev, 0x000020, 0xffffffff);
  277. reg_w(gspca_dev, 0x0620, 0);
  278. reg_w(gspca_dev, 0x0630, 0);
  279. reg_w(gspca_dev, 0x0640, 0);
  280. reg_w(gspca_dev, 0x0650, 0);
  281. reg_w(gspca_dev, 0x0660, 0);
  282. gspca_dbg(gspca_dev, D_STREAM, "camera stopped\n");
  283. }
  284. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  285. u8 *data, /* isoc packet */
  286. int len) /* iso packet length */
  287. {
  288. struct sd *sd = (struct sd *) gspca_dev;
  289. static unsigned char ffd9[] = {0xff, 0xd9};
  290. /* a frame starts with:
  291. * - 0xff 0xfe
  292. * - 0x08 0x00 - length (little endian ?!)
  293. * - 4 bytes = size of whole frame (BE - including header)
  294. * - 0x00 0x0c
  295. * - 0xff 0xd8
  296. * - .. JPEG image with escape sequences (ff 00)
  297. * (without ending - ff d9)
  298. */
  299. if (data[0] == 0xff && data[1] == 0xfe) {
  300. gspca_frame_add(gspca_dev, LAST_PACKET,
  301. ffd9, 2);
  302. /* put the JPEG 411 header */
  303. gspca_frame_add(gspca_dev, FIRST_PACKET,
  304. sd->jpeg_hdr, JPEG_HDR_SZ);
  305. /* beginning of the frame */
  306. #define STKHDRSZ 12
  307. data += STKHDRSZ;
  308. len -= STKHDRSZ;
  309. }
  310. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  311. }
  312. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  313. {
  314. struct gspca_dev *gspca_dev =
  315. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  316. gspca_dev->usb_err = 0;
  317. if (!gspca_dev->streaming)
  318. return 0;
  319. switch (ctrl->id) {
  320. case V4L2_CID_BRIGHTNESS:
  321. setbrightness(gspca_dev, ctrl->val);
  322. break;
  323. case V4L2_CID_CONTRAST:
  324. setcontrast(gspca_dev, ctrl->val);
  325. break;
  326. case V4L2_CID_SATURATION:
  327. setcolors(gspca_dev, ctrl->val);
  328. break;
  329. case V4L2_CID_POWER_LINE_FREQUENCY:
  330. setlightfreq(gspca_dev, ctrl->val);
  331. break;
  332. }
  333. return gspca_dev->usb_err;
  334. }
  335. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  336. .s_ctrl = sd_s_ctrl,
  337. };
  338. static int sd_init_controls(struct gspca_dev *gspca_dev)
  339. {
  340. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  341. gspca_dev->vdev.ctrl_handler = hdl;
  342. v4l2_ctrl_handler_init(hdl, 4);
  343. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  344. V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
  345. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  346. V4L2_CID_CONTRAST, 0, 255, 1, 127);
  347. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  348. V4L2_CID_SATURATION, 0, 255, 1, 127);
  349. v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
  350. V4L2_CID_POWER_LINE_FREQUENCY,
  351. V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 1,
  352. V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
  353. if (hdl->error) {
  354. pr_err("Could not initialize controls\n");
  355. return hdl->error;
  356. }
  357. return 0;
  358. }
  359. /* sub-driver description */
  360. static const struct sd_desc sd_desc = {
  361. .name = MODULE_NAME,
  362. .config = sd_config,
  363. .init = sd_init,
  364. .init_controls = sd_init_controls,
  365. .start = sd_start,
  366. .stopN = sd_stopN,
  367. .pkt_scan = sd_pkt_scan,
  368. };
  369. /* -- module initialisation -- */
  370. static const struct usb_device_id device_table[] = {
  371. {USB_DEVICE(0x05e1, 0x0893)},
  372. {}
  373. };
  374. MODULE_DEVICE_TABLE(usb, device_table);
  375. /* -- device connect -- */
  376. static int sd_probe(struct usb_interface *intf,
  377. const struct usb_device_id *id)
  378. {
  379. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  380. THIS_MODULE);
  381. }
  382. static struct usb_driver sd_driver = {
  383. .name = MODULE_NAME,
  384. .id_table = device_table,
  385. .probe = sd_probe,
  386. .disconnect = gspca_disconnect,
  387. #ifdef CONFIG_PM
  388. .suspend = gspca_suspend,
  389. .resume = gspca_resume,
  390. .reset_resume = gspca_resume,
  391. #endif
  392. };
  393. module_usb_driver(sd_driver);