spca1528.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * spca1528 subdriver
  3. *
  4. * Copyright (C) 2010-2011 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #define MODULE_NAME "spca1528"
  18. #include "gspca.h"
  19. #include "jpeg.h"
  20. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  21. MODULE_DESCRIPTION("SPCA1528 USB Camera Driver");
  22. MODULE_LICENSE("GPL");
  23. /* specific webcam descriptor */
  24. struct sd {
  25. struct gspca_dev gspca_dev; /* !! must be the first item */
  26. u8 pkt_seq;
  27. u8 jpeg_hdr[JPEG_HDR_SZ];
  28. };
  29. static const struct v4l2_pix_format vga_mode[] = {
  30. /* (does not work correctly)
  31. {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  32. .bytesperline = 176,
  33. .sizeimage = 176 * 144 * 5 / 8 + 590,
  34. .colorspace = V4L2_COLORSPACE_JPEG,
  35. .priv = 3},
  36. */
  37. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  38. .bytesperline = 320,
  39. .sizeimage = 320 * 240 * 4 / 8 + 590,
  40. .colorspace = V4L2_COLORSPACE_JPEG,
  41. .priv = 2},
  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 = 1},
  47. };
  48. /* read <len> bytes to gspca usb_buf */
  49. static void reg_r(struct gspca_dev *gspca_dev,
  50. u8 req,
  51. u16 index,
  52. int len)
  53. {
  54. #if USB_BUF_SZ < 64
  55. #error "USB buffer too small"
  56. #endif
  57. struct usb_device *dev = gspca_dev->dev;
  58. int ret;
  59. if (gspca_dev->usb_err < 0)
  60. return;
  61. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  62. req,
  63. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  64. 0x0000, /* value */
  65. index,
  66. gspca_dev->usb_buf, len,
  67. 500);
  68. gspca_dbg(gspca_dev, D_USBI, "GET %02x 0000 %04x %02x\n", req, index,
  69. gspca_dev->usb_buf[0]);
  70. if (ret < 0) {
  71. pr_err("reg_r err %d\n", ret);
  72. gspca_dev->usb_err = ret;
  73. }
  74. }
  75. static void reg_w(struct gspca_dev *gspca_dev,
  76. u8 req,
  77. u16 value,
  78. u16 index)
  79. {
  80. struct usb_device *dev = gspca_dev->dev;
  81. int ret;
  82. if (gspca_dev->usb_err < 0)
  83. return;
  84. gspca_dbg(gspca_dev, D_USBO, "SET %02x %04x %04x\n", req, value, index);
  85. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  86. req,
  87. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  88. value, index,
  89. NULL, 0, 500);
  90. if (ret < 0) {
  91. pr_err("reg_w err %d\n", ret);
  92. gspca_dev->usb_err = ret;
  93. }
  94. }
  95. static void reg_wb(struct gspca_dev *gspca_dev,
  96. u8 req,
  97. u16 value,
  98. u16 index,
  99. u8 byte)
  100. {
  101. struct usb_device *dev = gspca_dev->dev;
  102. int ret;
  103. if (gspca_dev->usb_err < 0)
  104. return;
  105. gspca_dbg(gspca_dev, D_USBO, "SET %02x %04x %04x %02x\n",
  106. req, value, index, byte);
  107. gspca_dev->usb_buf[0] = byte;
  108. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  109. req,
  110. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  111. value, index,
  112. gspca_dev->usb_buf, 1, 500);
  113. if (ret < 0) {
  114. pr_err("reg_w err %d\n", ret);
  115. gspca_dev->usb_err = ret;
  116. }
  117. }
  118. static void wait_status_0(struct gspca_dev *gspca_dev)
  119. {
  120. int i, w;
  121. i = 16;
  122. w = 0;
  123. do {
  124. reg_r(gspca_dev, 0x21, 0x0000, 1);
  125. if (gspca_dev->usb_buf[0] == 0)
  126. return;
  127. w += 15;
  128. msleep(w);
  129. } while (--i > 0);
  130. gspca_err(gspca_dev, "wait_status_0 timeout\n");
  131. gspca_dev->usb_err = -ETIME;
  132. }
  133. static void wait_status_1(struct gspca_dev *gspca_dev)
  134. {
  135. int i;
  136. i = 10;
  137. do {
  138. reg_r(gspca_dev, 0x21, 0x0001, 1);
  139. msleep(10);
  140. if (gspca_dev->usb_buf[0] == 1) {
  141. reg_wb(gspca_dev, 0x21, 0x0000, 0x0001, 0x00);
  142. reg_r(gspca_dev, 0x21, 0x0001, 1);
  143. return;
  144. }
  145. } while (--i > 0);
  146. gspca_err(gspca_dev, "wait_status_1 timeout\n");
  147. gspca_dev->usb_err = -ETIME;
  148. }
  149. static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
  150. {
  151. reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, val);
  152. }
  153. static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
  154. {
  155. reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, val);
  156. }
  157. static void sethue(struct gspca_dev *gspca_dev, s32 val)
  158. {
  159. reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, val);
  160. }
  161. static void setcolor(struct gspca_dev *gspca_dev, s32 val)
  162. {
  163. reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, val);
  164. }
  165. static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
  166. {
  167. reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, val);
  168. }
  169. /* this function is called at probe time */
  170. static int sd_config(struct gspca_dev *gspca_dev,
  171. const struct usb_device_id *id)
  172. {
  173. gspca_dev->cam.cam_mode = vga_mode;
  174. gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
  175. gspca_dev->cam.npkt = 128; /* number of packets per ISOC message */
  176. /*fixme: 256 in ms-win traces*/
  177. return 0;
  178. }
  179. /* this function is called at probe and resume time */
  180. static int sd_init(struct gspca_dev *gspca_dev)
  181. {
  182. reg_w(gspca_dev, 0x00, 0x0001, 0x2067);
  183. reg_w(gspca_dev, 0x00, 0x00d0, 0x206b);
  184. reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
  185. reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
  186. msleep(8);
  187. reg_w(gspca_dev, 0x00, 0x00c0, 0x206b);
  188. reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
  189. reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
  190. reg_r(gspca_dev, 0x20, 0x0000, 1);
  191. reg_r(gspca_dev, 0x20, 0x0000, 5);
  192. reg_r(gspca_dev, 0x23, 0x0000, 64);
  193. gspca_dbg(gspca_dev, D_PROBE, "%s%s\n", &gspca_dev->usb_buf[0x1c],
  194. &gspca_dev->usb_buf[0x30]);
  195. reg_r(gspca_dev, 0x23, 0x0001, 64);
  196. return gspca_dev->usb_err;
  197. }
  198. /* function called at start time before URB creation */
  199. static int sd_isoc_init(struct gspca_dev *gspca_dev)
  200. {
  201. u8 mode;
  202. reg_r(gspca_dev, 0x00, 0x2520, 1);
  203. wait_status_0(gspca_dev);
  204. reg_w(gspca_dev, 0xc5, 0x0003, 0x0000);
  205. wait_status_1(gspca_dev);
  206. wait_status_0(gspca_dev);
  207. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  208. reg_wb(gspca_dev, 0x25, 0x0000, 0x0004, mode);
  209. reg_r(gspca_dev, 0x25, 0x0004, 1);
  210. reg_wb(gspca_dev, 0x27, 0x0000, 0x0000, 0x06); /* 420 */
  211. reg_r(gspca_dev, 0x27, 0x0000, 1);
  212. /* not useful..
  213. gspca_dev->alt = 4; * use alternate setting 3 */
  214. return gspca_dev->usb_err;
  215. }
  216. /* -- start the camera -- */
  217. static int sd_start(struct gspca_dev *gspca_dev)
  218. {
  219. struct sd *sd = (struct sd *) gspca_dev;
  220. /* initialize the JPEG header */
  221. jpeg_define(sd->jpeg_hdr, gspca_dev->pixfmt.height,
  222. gspca_dev->pixfmt.width,
  223. 0x22); /* JPEG 411 */
  224. /* the JPEG quality shall be 85% */
  225. jpeg_set_qual(sd->jpeg_hdr, 85);
  226. reg_r(gspca_dev, 0x00, 0x2520, 1);
  227. msleep(8);
  228. /* start the capture */
  229. wait_status_0(gspca_dev);
  230. reg_w(gspca_dev, 0x31, 0x0000, 0x0004); /* start request */
  231. wait_status_1(gspca_dev);
  232. wait_status_0(gspca_dev);
  233. msleep(200);
  234. sd->pkt_seq = 0;
  235. return gspca_dev->usb_err;
  236. }
  237. static void sd_stopN(struct gspca_dev *gspca_dev)
  238. {
  239. /* stop the capture */
  240. wait_status_0(gspca_dev);
  241. reg_w(gspca_dev, 0x31, 0x0000, 0x0000); /* stop request */
  242. wait_status_1(gspca_dev);
  243. wait_status_0(gspca_dev);
  244. }
  245. /* move a packet adding 0x00 after 0xff */
  246. static void add_packet(struct gspca_dev *gspca_dev,
  247. u8 *data,
  248. int len)
  249. {
  250. int i;
  251. i = 0;
  252. do {
  253. if (data[i] == 0xff) {
  254. gspca_frame_add(gspca_dev, INTER_PACKET,
  255. data, i + 1);
  256. len -= i;
  257. data += i;
  258. *data = 0x00;
  259. i = 0;
  260. }
  261. } while (++i < len);
  262. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  263. }
  264. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  265. u8 *data, /* isoc packet */
  266. int len) /* iso packet length */
  267. {
  268. struct sd *sd = (struct sd *) gspca_dev;
  269. static const u8 ffd9[] = {0xff, 0xd9};
  270. /* image packets start with:
  271. * 02 8n
  272. * with <n> bit:
  273. * 0x01: even (0) / odd (1) image
  274. * 0x02: end of image when set
  275. */
  276. if (len < 3)
  277. return; /* empty packet */
  278. if (*data == 0x02) {
  279. if (data[1] & 0x02) {
  280. sd->pkt_seq = !(data[1] & 1);
  281. add_packet(gspca_dev, data + 2, len - 2);
  282. gspca_frame_add(gspca_dev, LAST_PACKET,
  283. ffd9, 2);
  284. return;
  285. }
  286. if ((data[1] & 1) != sd->pkt_seq)
  287. goto err;
  288. if (gspca_dev->last_packet_type == LAST_PACKET)
  289. gspca_frame_add(gspca_dev, FIRST_PACKET,
  290. sd->jpeg_hdr, JPEG_HDR_SZ);
  291. add_packet(gspca_dev, data + 2, len - 2);
  292. return;
  293. }
  294. err:
  295. gspca_dev->last_packet_type = DISCARD_PACKET;
  296. }
  297. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  298. {
  299. struct gspca_dev *gspca_dev =
  300. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  301. gspca_dev->usb_err = 0;
  302. if (!gspca_dev->streaming)
  303. return 0;
  304. switch (ctrl->id) {
  305. case V4L2_CID_BRIGHTNESS:
  306. setbrightness(gspca_dev, ctrl->val);
  307. break;
  308. case V4L2_CID_CONTRAST:
  309. setcontrast(gspca_dev, ctrl->val);
  310. break;
  311. case V4L2_CID_HUE:
  312. sethue(gspca_dev, ctrl->val);
  313. break;
  314. case V4L2_CID_SATURATION:
  315. setcolor(gspca_dev, ctrl->val);
  316. break;
  317. case V4L2_CID_SHARPNESS:
  318. setsharpness(gspca_dev, ctrl->val);
  319. break;
  320. }
  321. return gspca_dev->usb_err;
  322. }
  323. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  324. .s_ctrl = sd_s_ctrl,
  325. };
  326. static int sd_init_controls(struct gspca_dev *gspca_dev)
  327. {
  328. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  329. gspca_dev->vdev.ctrl_handler = hdl;
  330. v4l2_ctrl_handler_init(hdl, 5);
  331. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  332. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  333. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  334. V4L2_CID_CONTRAST, 0, 8, 1, 1);
  335. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  336. V4L2_CID_HUE, 0, 255, 1, 0);
  337. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  338. V4L2_CID_SATURATION, 0, 8, 1, 1);
  339. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  340. V4L2_CID_SHARPNESS, 0, 255, 1, 0);
  341. if (hdl->error) {
  342. pr_err("Could not initialize controls\n");
  343. return hdl->error;
  344. }
  345. return 0;
  346. }
  347. /* sub-driver description */
  348. static const struct sd_desc sd_desc = {
  349. .name = MODULE_NAME,
  350. .config = sd_config,
  351. .init = sd_init,
  352. .init_controls = sd_init_controls,
  353. .isoc_init = sd_isoc_init,
  354. .start = sd_start,
  355. .stopN = sd_stopN,
  356. .pkt_scan = sd_pkt_scan,
  357. };
  358. /* -- module initialisation -- */
  359. static const struct usb_device_id device_table[] = {
  360. {USB_DEVICE(0x04fc, 0x1528)},
  361. {}
  362. };
  363. MODULE_DEVICE_TABLE(usb, device_table);
  364. /* -- device connect -- */
  365. static int sd_probe(struct usb_interface *intf,
  366. const struct usb_device_id *id)
  367. {
  368. /* the video interface for isochronous transfer is 1 */
  369. if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
  370. return -ENODEV;
  371. return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
  372. THIS_MODULE);
  373. }
  374. static struct usb_driver sd_driver = {
  375. .name = MODULE_NAME,
  376. .id_table = device_table,
  377. .probe = sd_probe,
  378. .disconnect = gspca_disconnect,
  379. #ifdef CONFIG_PM
  380. .suspend = gspca_suspend,
  381. .resume = gspca_resume,
  382. .reset_resume = gspca_resume,
  383. #endif
  384. };
  385. module_usb_driver(sd_driver);