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, 5),
  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. struct cam *cam = &gspca_dev->cam;
  271. cam->epaddr = 0x02;
  272. gspca_dev->cam.cam_mode = vga_mode;
  273. gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
  274. sd->brightness = BRIGHTNESS_DEF;
  275. sd->contrast = CONTRAST_DEF;
  276. sd->colors = COLOR_DEF;
  277. sd->lightfreq = FREQ_DEF;
  278. return 0;
  279. }
  280. /* this function is called at probe and resume time */
  281. static int sd_init(struct gspca_dev *gspca_dev)
  282. {
  283. int ret;
  284. /* check if the device responds */
  285. usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
  286. ret = reg_r(gspca_dev, 0x0740);
  287. if (ret < 0)
  288. return ret;
  289. if (ret != 0xff) {
  290. PDEBUG(D_ERR|D_STREAM, "init reg: 0x%02x", ret);
  291. return -1;
  292. }
  293. return 0;
  294. }
  295. /* -- start the camera -- */
  296. static int sd_start(struct gspca_dev *gspca_dev)
  297. {
  298. int ret, value;
  299. /* work on alternate 1 */
  300. usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
  301. set_par(gspca_dev, 0x10000000);
  302. set_par(gspca_dev, 0x00000000);
  303. set_par(gspca_dev, 0x8002e001);
  304. set_par(gspca_dev, 0x14000000);
  305. if (gspca_dev->width > 320)
  306. value = 0x8002e001; /* 640x480 */
  307. else
  308. value = 0x4001f000; /* 320x240 */
  309. set_par(gspca_dev, value);
  310. ret = usb_set_interface(gspca_dev->dev,
  311. gspca_dev->iface,
  312. gspca_dev->alt);
  313. if (ret < 0) {
  314. PDEBUG(D_ERR|D_STREAM, "set intf %d %d failed",
  315. gspca_dev->iface, gspca_dev->alt);
  316. goto out;
  317. }
  318. ret = reg_r(gspca_dev, 0x0630);
  319. if (ret < 0)
  320. goto out;
  321. rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
  322. ret = reg_r(gspca_dev, 0x0650);
  323. if (ret < 0)
  324. goto out;
  325. snd_val(gspca_dev, 0x000020, 0xffffffff);
  326. reg_w(gspca_dev, 0x0620, 0);
  327. reg_w(gspca_dev, 0x0630, 0);
  328. reg_w(gspca_dev, 0x0640, 0);
  329. reg_w(gspca_dev, 0x0650, 0);
  330. reg_w(gspca_dev, 0x0660, 0);
  331. setbrightness(gspca_dev); /* whiteness */
  332. setcontrast(gspca_dev); /* contrast */
  333. setcolors(gspca_dev); /* saturation */
  334. set_par(gspca_dev, 0x09800000); /* Red ? */
  335. set_par(gspca_dev, 0x0a800000); /* Green ? */
  336. set_par(gspca_dev, 0x0b800000); /* Blue ? */
  337. set_par(gspca_dev, 0x0d030000); /* Gamma ? */
  338. setfreq(gspca_dev); /* light frequency */
  339. /* start the video flow */
  340. set_par(gspca_dev, 0x01000000);
  341. set_par(gspca_dev, 0x01000000);
  342. PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt);
  343. return 0;
  344. out:
  345. PDEBUG(D_ERR|D_STREAM, "camera start err %d", ret);
  346. return ret;
  347. }
  348. static void sd_stopN(struct gspca_dev *gspca_dev)
  349. {
  350. struct usb_device *dev = gspca_dev->dev;
  351. set_par(gspca_dev, 0x02000000);
  352. set_par(gspca_dev, 0x02000000);
  353. usb_set_interface(dev, gspca_dev->iface, 1);
  354. reg_r(gspca_dev, 0x0630);
  355. rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
  356. reg_r(gspca_dev, 0x0650);
  357. snd_val(gspca_dev, 0x000020, 0xffffffff);
  358. reg_w(gspca_dev, 0x0620, 0);
  359. reg_w(gspca_dev, 0x0630, 0);
  360. reg_w(gspca_dev, 0x0640, 0);
  361. reg_w(gspca_dev, 0x0650, 0);
  362. reg_w(gspca_dev, 0x0660, 0);
  363. PDEBUG(D_STREAM, "camera stopped");
  364. }
  365. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  366. struct gspca_frame *frame, /* target */
  367. __u8 *data, /* isoc packet */
  368. int len) /* iso packet length */
  369. {
  370. static unsigned char ffd9[] = {0xff, 0xd9};
  371. /* a frame starts with:
  372. * - 0xff 0xfe
  373. * - 0x08 0x00 - length (little endian ?!)
  374. * - 4 bytes = size of whole frame (BE - including header)
  375. * - 0x00 0x0c
  376. * - 0xff 0xd8
  377. * - .. JPEG image with escape sequences (ff 00)
  378. * (without ending - ff d9)
  379. */
  380. if (data[0] == 0xff && data[1] == 0xfe) {
  381. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  382. ffd9, 2);
  383. /* put the JPEG 411 header */
  384. jpeg_put_header(gspca_dev, frame, sd_quant, 0x22);
  385. /* beginning of the frame */
  386. #define STKHDRSZ 12
  387. data += STKHDRSZ;
  388. len -= STKHDRSZ;
  389. }
  390. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  391. }
  392. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  393. {
  394. struct sd *sd = (struct sd *) gspca_dev;
  395. sd->brightness = val;
  396. if (gspca_dev->streaming)
  397. setbrightness(gspca_dev);
  398. return 0;
  399. }
  400. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  401. {
  402. struct sd *sd = (struct sd *) gspca_dev;
  403. *val = sd->brightness;
  404. return 0;
  405. }
  406. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  407. {
  408. struct sd *sd = (struct sd *) gspca_dev;
  409. sd->contrast = val;
  410. if (gspca_dev->streaming)
  411. setcontrast(gspca_dev);
  412. return 0;
  413. }
  414. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  415. {
  416. struct sd *sd = (struct sd *) gspca_dev;
  417. *val = sd->contrast;
  418. return 0;
  419. }
  420. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
  421. {
  422. struct sd *sd = (struct sd *) gspca_dev;
  423. sd->colors = val;
  424. if (gspca_dev->streaming)
  425. setcolors(gspca_dev);
  426. return 0;
  427. }
  428. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
  429. {
  430. struct sd *sd = (struct sd *) gspca_dev;
  431. *val = sd->colors;
  432. return 0;
  433. }
  434. static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
  435. {
  436. struct sd *sd = (struct sd *) gspca_dev;
  437. sd->lightfreq = val;
  438. if (gspca_dev->streaming)
  439. setfreq(gspca_dev);
  440. return 0;
  441. }
  442. static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
  443. {
  444. struct sd *sd = (struct sd *) gspca_dev;
  445. *val = sd->lightfreq;
  446. return 0;
  447. }
  448. static int sd_querymenu(struct gspca_dev *gspca_dev,
  449. struct v4l2_querymenu *menu)
  450. {
  451. switch (menu->id) {
  452. case V4L2_CID_POWER_LINE_FREQUENCY:
  453. switch (menu->index) {
  454. case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
  455. strcpy((char *) menu->name, "50 Hz");
  456. return 0;
  457. case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
  458. strcpy((char *) menu->name, "60 Hz");
  459. return 0;
  460. }
  461. break;
  462. }
  463. return -EINVAL;
  464. }
  465. /* sub-driver description */
  466. static const struct sd_desc sd_desc = {
  467. .name = MODULE_NAME,
  468. .ctrls = sd_ctrls,
  469. .nctrls = ARRAY_SIZE(sd_ctrls),
  470. .config = sd_config,
  471. .init = sd_init,
  472. .start = sd_start,
  473. .stopN = sd_stopN,
  474. .pkt_scan = sd_pkt_scan,
  475. .querymenu = sd_querymenu,
  476. };
  477. /* -- module initialisation -- */
  478. static const __devinitdata struct usb_device_id device_table[] = {
  479. {USB_DEVICE(0x05e1, 0x0893)},
  480. {}
  481. };
  482. MODULE_DEVICE_TABLE(usb, device_table);
  483. /* -- device connect -- */
  484. static int sd_probe(struct usb_interface *intf,
  485. const struct usb_device_id *id)
  486. {
  487. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  488. THIS_MODULE);
  489. }
  490. static struct usb_driver sd_driver = {
  491. .name = MODULE_NAME,
  492. .id_table = device_table,
  493. .probe = sd_probe,
  494. .disconnect = gspca_disconnect,
  495. #ifdef CONFIG_PM
  496. .suspend = gspca_suspend,
  497. .resume = gspca_resume,
  498. #endif
  499. };
  500. /* -- module insert / remove -- */
  501. static int __init sd_mod_init(void)
  502. {
  503. if (usb_register(&sd_driver) < 0)
  504. return -1;
  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)");