stk014.c 11 KB

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