spca1528.c 10 KB

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