spca1528.c 10 KB

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