stk014.c 10 KB

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