stk014.c 12 KB

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