stk014.c 14 KB

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