stk014.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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 MODULE_NAME "stk014"
  21. #include "gspca.h"
  22. #include "jpeg.h"
  23. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  24. MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver");
  25. MODULE_LICENSE("GPL");
  26. /* specific webcam descriptor */
  27. struct sd {
  28. struct gspca_dev gspca_dev; /* !! must be the first item */
  29. unsigned char brightness;
  30. unsigned char contrast;
  31. unsigned char colors;
  32. unsigned char lightfreq;
  33. };
  34. /* global parameters */
  35. static int sd_quant = 7; /* <= 4 KO - 7: good (enough!) */
  36. /* V4L2 controls supported by the driver */
  37. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  38. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  39. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
  40. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
  41. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
  42. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
  43. static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
  44. static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
  45. static struct ctrl sd_ctrls[] = {
  46. {
  47. {
  48. .id = V4L2_CID_BRIGHTNESS,
  49. .type = V4L2_CTRL_TYPE_INTEGER,
  50. .name = "Brightness",
  51. .minimum = 0,
  52. .maximum = 255,
  53. .step = 1,
  54. #define BRIGHTNESS_DEF 127
  55. .default_value = BRIGHTNESS_DEF,
  56. },
  57. .set = sd_setbrightness,
  58. .get = sd_getbrightness,
  59. },
  60. {
  61. {
  62. .id = V4L2_CID_CONTRAST,
  63. .type = V4L2_CTRL_TYPE_INTEGER,
  64. .name = "Contrast",
  65. .minimum = 0,
  66. .maximum = 255,
  67. .step = 1,
  68. #define CONTRAST_DEF 127
  69. .default_value = CONTRAST_DEF,
  70. },
  71. .set = sd_setcontrast,
  72. .get = sd_getcontrast,
  73. },
  74. {
  75. {
  76. .id = V4L2_CID_SATURATION,
  77. .type = V4L2_CTRL_TYPE_INTEGER,
  78. .name = "Color",
  79. .minimum = 0,
  80. .maximum = 255,
  81. .step = 1,
  82. #define COLOR_DEF 127
  83. .default_value = COLOR_DEF,
  84. },
  85. .set = sd_setcolors,
  86. .get = sd_getcolors,
  87. },
  88. {
  89. {
  90. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  91. .type = V4L2_CTRL_TYPE_MENU,
  92. .name = "Light frequency filter",
  93. .minimum = 1,
  94. .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
  95. .step = 1,
  96. #define FREQ_DEF 1
  97. .default_value = FREQ_DEF,
  98. },
  99. .set = sd_setfreq,
  100. .get = sd_getfreq,
  101. },
  102. };
  103. static const struct v4l2_pix_format vga_mode[] = {
  104. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  105. .bytesperline = 320,
  106. .sizeimage = 320 * 240 * 3 / 8 + 590,
  107. .colorspace = V4L2_COLORSPACE_JPEG,
  108. .priv = 1},
  109. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  110. .bytesperline = 640,
  111. .sizeimage = 640 * 480 * 3 / 8 + 590,
  112. .colorspace = V4L2_COLORSPACE_JPEG,
  113. .priv = 0},
  114. };
  115. /* -- read a register -- */
  116. static int reg_r(struct gspca_dev *gspca_dev,
  117. __u16 index)
  118. {
  119. struct usb_device *dev = gspca_dev->dev;
  120. int ret;
  121. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  122. 0x00,
  123. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  124. 0x00,
  125. index,
  126. gspca_dev->usb_buf, 1,
  127. 500);
  128. if (ret < 0) {
  129. PDEBUG(D_ERR, "reg_r err %d", ret);
  130. return ret;
  131. }
  132. return gspca_dev->usb_buf[0];
  133. }
  134. /* -- write a register -- */
  135. static int reg_w(struct gspca_dev *gspca_dev,
  136. __u16 index, __u16 value)
  137. {
  138. struct usb_device *dev = gspca_dev->dev;
  139. int ret;
  140. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  141. 0x01,
  142. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  143. value,
  144. index,
  145. NULL,
  146. 0,
  147. 500);
  148. if (ret < 0)
  149. PDEBUG(D_ERR, "reg_w err %d", ret);
  150. return ret;
  151. }
  152. /* -- get a bulk value (4 bytes) -- */
  153. static int rcv_val(struct gspca_dev *gspca_dev,
  154. int ads)
  155. {
  156. struct usb_device *dev = gspca_dev->dev;
  157. int alen, ret;
  158. reg_w(gspca_dev, 0x634, (ads >> 16) & 0xff);
  159. reg_w(gspca_dev, 0x635, (ads >> 8) & 0xff);
  160. reg_w(gspca_dev, 0x636, ads & 0xff);
  161. reg_w(gspca_dev, 0x637, 0);
  162. reg_w(gspca_dev, 0x638, 4); /* len & 0xff */
  163. reg_w(gspca_dev, 0x639, 0); /* len >> 8 */
  164. reg_w(gspca_dev, 0x63a, 0);
  165. reg_w(gspca_dev, 0x63b, 0);
  166. reg_w(gspca_dev, 0x630, 5);
  167. ret = usb_bulk_msg(dev,
  168. usb_rcvbulkpipe(dev, 0x05),
  169. gspca_dev->usb_buf,
  170. 4, /* length */
  171. &alen,
  172. 500); /* timeout in milliseconds */
  173. return ret;
  174. }
  175. /* -- send a bulk value -- */
  176. static int snd_val(struct gspca_dev *gspca_dev,
  177. int ads,
  178. unsigned int val)
  179. {
  180. struct usb_device *dev = gspca_dev->dev;
  181. int alen, ret;
  182. __u8 seq = 0;
  183. if (ads == 0x003f08) {
  184. ret = reg_r(gspca_dev, 0x0704);
  185. if (ret < 0)
  186. goto ko;
  187. ret = reg_r(gspca_dev, 0x0705);
  188. if (ret < 0)
  189. goto ko;
  190. seq = ret; /* keep the sequence number */
  191. ret = reg_r(gspca_dev, 0x0650);
  192. if (ret < 0)
  193. goto ko;
  194. reg_w(gspca_dev, 0x654, seq);
  195. } else {
  196. reg_w(gspca_dev, 0x654, (ads >> 16) & 0xff);
  197. }
  198. reg_w(gspca_dev, 0x655, (ads >> 8) & 0xff);
  199. reg_w(gspca_dev, 0x656, ads & 0xff);
  200. reg_w(gspca_dev, 0x657, 0);
  201. reg_w(gspca_dev, 0x658, 0x04); /* size */
  202. reg_w(gspca_dev, 0x659, 0);
  203. reg_w(gspca_dev, 0x65a, 0);
  204. reg_w(gspca_dev, 0x65b, 0);
  205. reg_w(gspca_dev, 0x650, 5);
  206. gspca_dev->usb_buf[0] = val >> 24;
  207. gspca_dev->usb_buf[1] = val >> 16;
  208. gspca_dev->usb_buf[2] = val >> 8;
  209. gspca_dev->usb_buf[3] = val;
  210. ret = usb_bulk_msg(dev,
  211. usb_sndbulkpipe(dev, 6),
  212. gspca_dev->usb_buf,
  213. 4,
  214. &alen,
  215. 500); /* timeout in milliseconds */
  216. if (ret < 0)
  217. goto ko;
  218. if (ads == 0x003f08) {
  219. seq += 4;
  220. seq &= 0x3f;
  221. reg_w(gspca_dev, 0x705, seq);
  222. }
  223. return ret;
  224. ko:
  225. PDEBUG(D_ERR, "snd_val err %d", ret);
  226. return ret;
  227. }
  228. /* set a camera parameter */
  229. static int set_par(struct gspca_dev *gspca_dev,
  230. int parval)
  231. {
  232. return snd_val(gspca_dev, 0x003f08, parval);
  233. }
  234. static void setbrightness(struct gspca_dev *gspca_dev)
  235. {
  236. struct sd *sd = (struct sd *) gspca_dev;
  237. int parval;
  238. parval = 0x06000000 /* whiteness */
  239. + (sd->brightness << 16);
  240. set_par(gspca_dev, parval);
  241. }
  242. static void setcontrast(struct gspca_dev *gspca_dev)
  243. {
  244. struct sd *sd = (struct sd *) gspca_dev;
  245. int parval;
  246. parval = 0x07000000 /* contrast */
  247. + (sd->contrast << 16);
  248. set_par(gspca_dev, parval);
  249. }
  250. static void setcolors(struct gspca_dev *gspca_dev)
  251. {
  252. struct sd *sd = (struct sd *) gspca_dev;
  253. int parval;
  254. parval = 0x08000000 /* saturation */
  255. + (sd->colors << 16);
  256. set_par(gspca_dev, parval);
  257. }
  258. static void setfreq(struct gspca_dev *gspca_dev)
  259. {
  260. struct sd *sd = (struct sd *) gspca_dev;
  261. set_par(gspca_dev, sd->lightfreq == 1
  262. ? 0x33640000 /* 50 Hz */
  263. : 0x33780000); /* 60 Hz */
  264. }
  265. /* this function is called at probe time */
  266. static int sd_config(struct gspca_dev *gspca_dev,
  267. const struct usb_device_id *id)
  268. {
  269. struct sd *sd = (struct sd *) gspca_dev;
  270. gspca_dev->cam.cam_mode = vga_mode;
  271. gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
  272. sd->brightness = BRIGHTNESS_DEF;
  273. sd->contrast = CONTRAST_DEF;
  274. sd->colors = COLOR_DEF;
  275. sd->lightfreq = FREQ_DEF;
  276. return 0;
  277. }
  278. /* this function is called at probe and resume time */
  279. static int sd_init(struct gspca_dev *gspca_dev)
  280. {
  281. int ret;
  282. /* check if the device responds */
  283. usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
  284. ret = reg_r(gspca_dev, 0x0740);
  285. if (ret < 0)
  286. return ret;
  287. if (ret != 0xff) {
  288. PDEBUG(D_ERR|D_STREAM, "init reg: 0x%02x", ret);
  289. return -1;
  290. }
  291. return 0;
  292. }
  293. /* -- start the camera -- */
  294. static int sd_start(struct gspca_dev *gspca_dev)
  295. {
  296. int ret, value;
  297. /* work on alternate 1 */
  298. usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
  299. set_par(gspca_dev, 0x10000000);
  300. set_par(gspca_dev, 0x00000000);
  301. set_par(gspca_dev, 0x8002e001);
  302. set_par(gspca_dev, 0x14000000);
  303. if (gspca_dev->width > 320)
  304. value = 0x8002e001; /* 640x480 */
  305. else
  306. value = 0x4001f000; /* 320x240 */
  307. set_par(gspca_dev, value);
  308. ret = usb_set_interface(gspca_dev->dev,
  309. gspca_dev->iface,
  310. gspca_dev->alt);
  311. if (ret < 0) {
  312. PDEBUG(D_ERR|D_STREAM, "set intf %d %d failed",
  313. gspca_dev->iface, gspca_dev->alt);
  314. goto out;
  315. }
  316. ret = reg_r(gspca_dev, 0x0630);
  317. if (ret < 0)
  318. goto out;
  319. rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
  320. ret = reg_r(gspca_dev, 0x0650);
  321. if (ret < 0)
  322. goto out;
  323. snd_val(gspca_dev, 0x000020, 0xffffffff);
  324. reg_w(gspca_dev, 0x0620, 0);
  325. reg_w(gspca_dev, 0x0630, 0);
  326. reg_w(gspca_dev, 0x0640, 0);
  327. reg_w(gspca_dev, 0x0650, 0);
  328. reg_w(gspca_dev, 0x0660, 0);
  329. setbrightness(gspca_dev); /* whiteness */
  330. setcontrast(gspca_dev); /* contrast */
  331. setcolors(gspca_dev); /* saturation */
  332. set_par(gspca_dev, 0x09800000); /* Red ? */
  333. set_par(gspca_dev, 0x0a800000); /* Green ? */
  334. set_par(gspca_dev, 0x0b800000); /* Blue ? */
  335. set_par(gspca_dev, 0x0d030000); /* Gamma ? */
  336. setfreq(gspca_dev); /* light frequency */
  337. /* start the video flow */
  338. set_par(gspca_dev, 0x01000000);
  339. set_par(gspca_dev, 0x01000000);
  340. PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt);
  341. return 0;
  342. out:
  343. PDEBUG(D_ERR|D_STREAM, "camera start err %d", ret);
  344. return ret;
  345. }
  346. static void sd_stopN(struct gspca_dev *gspca_dev)
  347. {
  348. struct usb_device *dev = gspca_dev->dev;
  349. set_par(gspca_dev, 0x02000000);
  350. set_par(gspca_dev, 0x02000000);
  351. usb_set_interface(dev, gspca_dev->iface, 1);
  352. reg_r(gspca_dev, 0x0630);
  353. rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
  354. reg_r(gspca_dev, 0x0650);
  355. snd_val(gspca_dev, 0x000020, 0xffffffff);
  356. reg_w(gspca_dev, 0x0620, 0);
  357. reg_w(gspca_dev, 0x0630, 0);
  358. reg_w(gspca_dev, 0x0640, 0);
  359. reg_w(gspca_dev, 0x0650, 0);
  360. reg_w(gspca_dev, 0x0660, 0);
  361. PDEBUG(D_STREAM, "camera stopped");
  362. }
  363. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  364. struct gspca_frame *frame, /* target */
  365. __u8 *data, /* isoc packet */
  366. int len) /* iso packet length */
  367. {
  368. static unsigned char ffd9[] = {0xff, 0xd9};
  369. /* a frame starts with:
  370. * - 0xff 0xfe
  371. * - 0x08 0x00 - length (little endian ?!)
  372. * - 4 bytes = size of whole frame (BE - including header)
  373. * - 0x00 0x0c
  374. * - 0xff 0xd8
  375. * - .. JPEG image with escape sequences (ff 00)
  376. * (without ending - ff d9)
  377. */
  378. if (data[0] == 0xff && data[1] == 0xfe) {
  379. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  380. ffd9, 2);
  381. /* put the JPEG 411 header */
  382. jpeg_put_header(gspca_dev, frame, sd_quant, 0x22);
  383. /* beginning of the frame */
  384. #define STKHDRSZ 12
  385. data += STKHDRSZ;
  386. len -= STKHDRSZ;
  387. }
  388. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  389. }
  390. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  391. {
  392. struct sd *sd = (struct sd *) gspca_dev;
  393. sd->brightness = val;
  394. if (gspca_dev->streaming)
  395. setbrightness(gspca_dev);
  396. return 0;
  397. }
  398. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  399. {
  400. struct sd *sd = (struct sd *) gspca_dev;
  401. *val = sd->brightness;
  402. return 0;
  403. }
  404. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  405. {
  406. struct sd *sd = (struct sd *) gspca_dev;
  407. sd->contrast = val;
  408. if (gspca_dev->streaming)
  409. setcontrast(gspca_dev);
  410. return 0;
  411. }
  412. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  413. {
  414. struct sd *sd = (struct sd *) gspca_dev;
  415. *val = sd->contrast;
  416. return 0;
  417. }
  418. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
  419. {
  420. struct sd *sd = (struct sd *) gspca_dev;
  421. sd->colors = val;
  422. if (gspca_dev->streaming)
  423. setcolors(gspca_dev);
  424. return 0;
  425. }
  426. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
  427. {
  428. struct sd *sd = (struct sd *) gspca_dev;
  429. *val = sd->colors;
  430. return 0;
  431. }
  432. static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
  433. {
  434. struct sd *sd = (struct sd *) gspca_dev;
  435. sd->lightfreq = val;
  436. if (gspca_dev->streaming)
  437. setfreq(gspca_dev);
  438. return 0;
  439. }
  440. static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
  441. {
  442. struct sd *sd = (struct sd *) gspca_dev;
  443. *val = sd->lightfreq;
  444. return 0;
  445. }
  446. static int sd_querymenu(struct gspca_dev *gspca_dev,
  447. struct v4l2_querymenu *menu)
  448. {
  449. switch (menu->id) {
  450. case V4L2_CID_POWER_LINE_FREQUENCY:
  451. switch (menu->index) {
  452. case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
  453. strcpy((char *) menu->name, "50 Hz");
  454. return 0;
  455. case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
  456. strcpy((char *) menu->name, "60 Hz");
  457. return 0;
  458. }
  459. break;
  460. }
  461. return -EINVAL;
  462. }
  463. /* sub-driver description */
  464. static const struct sd_desc sd_desc = {
  465. .name = MODULE_NAME,
  466. .ctrls = sd_ctrls,
  467. .nctrls = ARRAY_SIZE(sd_ctrls),
  468. .config = sd_config,
  469. .init = sd_init,
  470. .start = sd_start,
  471. .stopN = sd_stopN,
  472. .pkt_scan = sd_pkt_scan,
  473. .querymenu = sd_querymenu,
  474. };
  475. /* -- module initialisation -- */
  476. static const __devinitdata struct usb_device_id device_table[] = {
  477. {USB_DEVICE(0x05e1, 0x0893)},
  478. {}
  479. };
  480. MODULE_DEVICE_TABLE(usb, device_table);
  481. /* -- device connect -- */
  482. static int sd_probe(struct usb_interface *intf,
  483. const struct usb_device_id *id)
  484. {
  485. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  486. THIS_MODULE);
  487. }
  488. static struct usb_driver sd_driver = {
  489. .name = MODULE_NAME,
  490. .id_table = device_table,
  491. .probe = sd_probe,
  492. .disconnect = gspca_disconnect,
  493. #ifdef CONFIG_PM
  494. .suspend = gspca_suspend,
  495. .resume = gspca_resume,
  496. #endif
  497. };
  498. /* -- module insert / remove -- */
  499. static int __init sd_mod_init(void)
  500. {
  501. int ret;
  502. ret = usb_register(&sd_driver);
  503. if (ret < 0)
  504. return ret;
  505. info("registered");
  506. return 0;
  507. }
  508. static void __exit sd_mod_exit(void)
  509. {
  510. usb_deregister(&sd_driver);
  511. info("deregistered");
  512. }
  513. module_init(sd_mod_init);
  514. module_exit(sd_mod_exit);
  515. module_param_named(quant, sd_quant, int, 0644);
  516. MODULE_PARM_DESC(quant, "Quantization index (0..8)");