spca1528.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. PDEBUG(D_ERR, "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. PDEBUG(D_ERR, "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->height, gspca_dev->width,
  225. 0x22); /* JPEG 411 */
  226. /* the JPEG quality shall be 85% */
  227. jpeg_set_qual(sd->jpeg_hdr, 85);
  228. reg_r(gspca_dev, 0x00, 0x2520, 1);
  229. msleep(8);
  230. /* start the capture */
  231. wait_status_0(gspca_dev);
  232. reg_w(gspca_dev, 0x31, 0x0000, 0x0004); /* start request */
  233. wait_status_1(gspca_dev);
  234. wait_status_0(gspca_dev);
  235. msleep(200);
  236. sd->pkt_seq = 0;
  237. return gspca_dev->usb_err;
  238. }
  239. static void sd_stopN(struct gspca_dev *gspca_dev)
  240. {
  241. /* stop the capture */
  242. wait_status_0(gspca_dev);
  243. reg_w(gspca_dev, 0x31, 0x0000, 0x0000); /* stop request */
  244. wait_status_1(gspca_dev);
  245. wait_status_0(gspca_dev);
  246. }
  247. /* move a packet adding 0x00 after 0xff */
  248. static void add_packet(struct gspca_dev *gspca_dev,
  249. u8 *data,
  250. int len)
  251. {
  252. int i;
  253. i = 0;
  254. do {
  255. if (data[i] == 0xff) {
  256. gspca_frame_add(gspca_dev, INTER_PACKET,
  257. data, i + 1);
  258. len -= i;
  259. data += i;
  260. *data = 0x00;
  261. i = 0;
  262. }
  263. } while (++i < len);
  264. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  265. }
  266. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  267. u8 *data, /* isoc packet */
  268. int len) /* iso packet length */
  269. {
  270. struct sd *sd = (struct sd *) gspca_dev;
  271. static const u8 ffd9[] = {0xff, 0xd9};
  272. /* image packets start with:
  273. * 02 8n
  274. * with <n> bit:
  275. * 0x01: even (0) / odd (1) image
  276. * 0x02: end of image when set
  277. */
  278. if (len < 3)
  279. return; /* empty packet */
  280. if (*data == 0x02) {
  281. if (data[1] & 0x02) {
  282. sd->pkt_seq = !(data[1] & 1);
  283. add_packet(gspca_dev, data + 2, len - 2);
  284. gspca_frame_add(gspca_dev, LAST_PACKET,
  285. ffd9, 2);
  286. return;
  287. }
  288. if ((data[1] & 1) != sd->pkt_seq)
  289. goto err;
  290. if (gspca_dev->last_packet_type == LAST_PACKET)
  291. gspca_frame_add(gspca_dev, FIRST_PACKET,
  292. sd->jpeg_hdr, JPEG_HDR_SZ);
  293. add_packet(gspca_dev, data + 2, len - 2);
  294. return;
  295. }
  296. err:
  297. gspca_dev->last_packet_type = DISCARD_PACKET;
  298. }
  299. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  300. {
  301. struct gspca_dev *gspca_dev =
  302. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  303. gspca_dev->usb_err = 0;
  304. if (!gspca_dev->streaming)
  305. return 0;
  306. switch (ctrl->id) {
  307. case V4L2_CID_BRIGHTNESS:
  308. setbrightness(gspca_dev, ctrl->val);
  309. break;
  310. case V4L2_CID_CONTRAST:
  311. setcontrast(gspca_dev, ctrl->val);
  312. break;
  313. case V4L2_CID_HUE:
  314. sethue(gspca_dev, ctrl->val);
  315. break;
  316. case V4L2_CID_SATURATION:
  317. setcolor(gspca_dev, ctrl->val);
  318. break;
  319. case V4L2_CID_SHARPNESS:
  320. setsharpness(gspca_dev, ctrl->val);
  321. break;
  322. }
  323. return gspca_dev->usb_err;
  324. }
  325. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  326. .s_ctrl = sd_s_ctrl,
  327. };
  328. static int sd_init_controls(struct gspca_dev *gspca_dev)
  329. {
  330. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  331. gspca_dev->vdev.ctrl_handler = hdl;
  332. v4l2_ctrl_handler_init(hdl, 5);
  333. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  334. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  335. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  336. V4L2_CID_CONTRAST, 0, 8, 1, 1);
  337. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  338. V4L2_CID_HUE, 0, 255, 1, 0);
  339. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  340. V4L2_CID_SATURATION, 0, 8, 1, 1);
  341. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  342. V4L2_CID_SHARPNESS, 0, 255, 1, 0);
  343. if (hdl->error) {
  344. pr_err("Could not initialize controls\n");
  345. return hdl->error;
  346. }
  347. return 0;
  348. }
  349. /* sub-driver description */
  350. static const struct sd_desc sd_desc = {
  351. .name = MODULE_NAME,
  352. .config = sd_config,
  353. .init = sd_init,
  354. .init_controls = sd_init_controls,
  355. .isoc_init = sd_isoc_init,
  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(0x04fc, 0x1528)},
  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. /* the video interface for isochronous transfer is 1 */
  371. if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
  372. return -ENODEV;
  373. return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
  374. THIS_MODULE);
  375. }
  376. static struct usb_driver sd_driver = {
  377. .name = MODULE_NAME,
  378. .id_table = device_table,
  379. .probe = sd_probe,
  380. .disconnect = gspca_disconnect,
  381. #ifdef CONFIG_PM
  382. .suspend = gspca_suspend,
  383. .resume = gspca_resume,
  384. .reset_resume = gspca_resume,
  385. #endif
  386. };
  387. module_usb_driver(sd_driver);