spca1528.c 10 KB

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