finepix.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Fujifilm Finepix subdriver
  3. *
  4. * Copyright (C) 2008 Frank Zago
  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 MODULE_NAME "finepix"
  21. #include "gspca.h"
  22. MODULE_AUTHOR("Frank Zago <frank@zago.net>");
  23. MODULE_DESCRIPTION("Fujifilm FinePix USB V4L2 driver");
  24. MODULE_LICENSE("GPL");
  25. /* Default timeout, in ms */
  26. #define FPIX_TIMEOUT 250
  27. /* Maximum transfer size to use. The windows driver reads by chunks of
  28. * 0x2000 bytes, so do the same. Note: reading more seems to work
  29. * too. */
  30. #define FPIX_MAX_TRANSFER 0x2000
  31. /* Structure to hold all of our device specific stuff */
  32. struct usb_fpix {
  33. struct gspca_dev gspca_dev; /* !! must be the first item */
  34. struct work_struct work_struct;
  35. struct workqueue_struct *work_thread;
  36. };
  37. /* Delay after which claim the next frame. If the delay is too small,
  38. * the camera will return old frames. On the 4800Z, 20ms is bad, 25ms
  39. * will fail every 4 or 5 frames, but 30ms is perfect. On the A210,
  40. * 30ms is bad while 35ms is perfect. */
  41. #define NEXT_FRAME_DELAY 35
  42. /* These cameras only support 320x200. */
  43. static const struct v4l2_pix_format fpix_mode[1] = {
  44. { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  45. .bytesperline = 320,
  46. .sizeimage = 320 * 240 * 3 / 8 + 590,
  47. .colorspace = V4L2_COLORSPACE_SRGB,
  48. .priv = 0}
  49. };
  50. /* send a command to the webcam */
  51. static int command(struct gspca_dev *gspca_dev,
  52. int order) /* 0: reset, 1: frame request */
  53. {
  54. static u8 order_values[2][12] = {
  55. {0xc6, 0, 0, 0, 0, 0, 0, 0, 0x20, 0, 0, 0}, /* reset */
  56. {0xd3, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0}, /* fr req */
  57. };
  58. memcpy(gspca_dev->usb_buf, order_values[order], 12);
  59. return usb_control_msg(gspca_dev->dev,
  60. usb_sndctrlpipe(gspca_dev->dev, 0),
  61. USB_REQ_GET_STATUS,
  62. USB_DIR_OUT | USB_TYPE_CLASS |
  63. USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
  64. 12, FPIX_TIMEOUT);
  65. }
  66. /* workqueue */
  67. static void dostream(struct work_struct *work)
  68. {
  69. struct usb_fpix *dev = container_of(work, struct usb_fpix, work_struct);
  70. struct gspca_dev *gspca_dev = &dev->gspca_dev;
  71. struct urb *urb = gspca_dev->urb[0];
  72. u8 *data = urb->transfer_buffer;
  73. int ret = 0;
  74. int len;
  75. /* synchronize with the main driver */
  76. mutex_lock(&gspca_dev->usb_lock);
  77. mutex_unlock(&gspca_dev->usb_lock);
  78. PDEBUG(D_STREAM, "dostream started");
  79. /* loop reading a frame */
  80. again:
  81. while (gspca_dev->present && gspca_dev->streaming) {
  82. /* request a frame */
  83. mutex_lock(&gspca_dev->usb_lock);
  84. ret = command(gspca_dev, 1);
  85. mutex_unlock(&gspca_dev->usb_lock);
  86. if (ret < 0)
  87. break;
  88. if (!gspca_dev->present || !gspca_dev->streaming)
  89. break;
  90. /* the frame comes in parts */
  91. for (;;) {
  92. ret = usb_bulk_msg(gspca_dev->dev,
  93. urb->pipe,
  94. data,
  95. FPIX_MAX_TRANSFER,
  96. &len, FPIX_TIMEOUT);
  97. if (ret < 0) {
  98. /* Most of the time we get a timeout
  99. * error. Just restart. */
  100. goto again;
  101. }
  102. if (!gspca_dev->present || !gspca_dev->streaming)
  103. goto out;
  104. if (len < FPIX_MAX_TRANSFER ||
  105. (data[len - 2] == 0xff &&
  106. data[len - 1] == 0xd9)) {
  107. /* If the result is less than what was asked
  108. * for, then it's the end of the
  109. * frame. Sometimes the jpeg is not complete,
  110. * but there's nothing we can do. We also end
  111. * here if the the jpeg ends right at the end
  112. * of the frame. */
  113. gspca_frame_add(gspca_dev, LAST_PACKET,
  114. data, len);
  115. break;
  116. }
  117. /* got a partial image */
  118. gspca_frame_add(gspca_dev,
  119. gspca_dev->last_packet_type
  120. == LAST_PACKET
  121. ? FIRST_PACKET : INTER_PACKET,
  122. data, len);
  123. }
  124. /* We must wait before trying reading the next
  125. * frame. If we don't, or if the delay is too short,
  126. * the camera will disconnect. */
  127. msleep(NEXT_FRAME_DELAY);
  128. }
  129. out:
  130. PDEBUG(D_STREAM, "dostream stopped");
  131. }
  132. /* this function is called at probe time */
  133. static int sd_config(struct gspca_dev *gspca_dev,
  134. const struct usb_device_id *id)
  135. {
  136. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  137. struct cam *cam = &gspca_dev->cam;
  138. cam->cam_mode = fpix_mode;
  139. cam->nmodes = 1;
  140. cam->bulk = 1;
  141. cam->bulk_size = FPIX_MAX_TRANSFER;
  142. INIT_WORK(&dev->work_struct, dostream);
  143. return 0;
  144. }
  145. /* this function is called at probe and resume time */
  146. static int sd_init(struct gspca_dev *gspca_dev)
  147. {
  148. return 0;
  149. }
  150. /* start the camera */
  151. static int sd_start(struct gspca_dev *gspca_dev)
  152. {
  153. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  154. int ret, len;
  155. /* Init the device */
  156. ret = command(gspca_dev, 0);
  157. if (ret < 0) {
  158. PDEBUG(D_STREAM, "init failed %d", ret);
  159. return ret;
  160. }
  161. /* Read the result of the command. Ignore the result, for it
  162. * varies with the device. */
  163. ret = usb_bulk_msg(gspca_dev->dev,
  164. gspca_dev->urb[0]->pipe,
  165. gspca_dev->urb[0]->transfer_buffer,
  166. FPIX_MAX_TRANSFER, &len,
  167. FPIX_TIMEOUT);
  168. if (ret < 0) {
  169. PDEBUG(D_STREAM, "usb_bulk_msg failed %d", ret);
  170. return ret;
  171. }
  172. /* Request a frame, but don't read it */
  173. ret = command(gspca_dev, 1);
  174. if (ret < 0) {
  175. PDEBUG(D_STREAM, "frame request failed %d", ret);
  176. return ret;
  177. }
  178. /* Again, reset bulk in endpoint */
  179. usb_clear_halt(gspca_dev->dev, gspca_dev->urb[0]->pipe);
  180. /* Start the workqueue function to do the streaming */
  181. dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
  182. queue_work(dev->work_thread, &dev->work_struct);
  183. return 0;
  184. }
  185. /* called on streamoff with alt==0 and on disconnect */
  186. /* the usb_lock is held at entry - restore on exit */
  187. static void sd_stop0(struct gspca_dev *gspca_dev)
  188. {
  189. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  190. /* wait for the work queue to terminate */
  191. mutex_unlock(&gspca_dev->usb_lock);
  192. destroy_workqueue(dev->work_thread);
  193. mutex_lock(&gspca_dev->usb_lock);
  194. dev->work_thread = NULL;
  195. }
  196. /* Table of supported USB devices */
  197. static const __devinitdata struct usb_device_id device_table[] = {
  198. {USB_DEVICE(0x04cb, 0x0104)},
  199. {USB_DEVICE(0x04cb, 0x0109)},
  200. {USB_DEVICE(0x04cb, 0x010b)},
  201. {USB_DEVICE(0x04cb, 0x010f)},
  202. {USB_DEVICE(0x04cb, 0x0111)},
  203. {USB_DEVICE(0x04cb, 0x0113)},
  204. {USB_DEVICE(0x04cb, 0x0115)},
  205. {USB_DEVICE(0x04cb, 0x0117)},
  206. {USB_DEVICE(0x04cb, 0x0119)},
  207. {USB_DEVICE(0x04cb, 0x011b)},
  208. {USB_DEVICE(0x04cb, 0x011d)},
  209. {USB_DEVICE(0x04cb, 0x0121)},
  210. {USB_DEVICE(0x04cb, 0x0123)},
  211. {USB_DEVICE(0x04cb, 0x0125)},
  212. {USB_DEVICE(0x04cb, 0x0127)},
  213. {USB_DEVICE(0x04cb, 0x0129)},
  214. {USB_DEVICE(0x04cb, 0x012b)},
  215. {USB_DEVICE(0x04cb, 0x012d)},
  216. {USB_DEVICE(0x04cb, 0x012f)},
  217. {USB_DEVICE(0x04cb, 0x0131)},
  218. {USB_DEVICE(0x04cb, 0x013b)},
  219. {USB_DEVICE(0x04cb, 0x013d)},
  220. {USB_DEVICE(0x04cb, 0x013f)},
  221. {}
  222. };
  223. MODULE_DEVICE_TABLE(usb, device_table);
  224. /* sub-driver description */
  225. static const struct sd_desc sd_desc = {
  226. .name = MODULE_NAME,
  227. .config = sd_config,
  228. .init = sd_init,
  229. .start = sd_start,
  230. .stop0 = sd_stop0,
  231. };
  232. /* -- device connect -- */
  233. static int sd_probe(struct usb_interface *intf,
  234. const struct usb_device_id *id)
  235. {
  236. return gspca_dev_probe(intf, id,
  237. &sd_desc,
  238. sizeof(struct usb_fpix),
  239. THIS_MODULE);
  240. }
  241. static struct usb_driver sd_driver = {
  242. .name = MODULE_NAME,
  243. .id_table = device_table,
  244. .probe = sd_probe,
  245. .disconnect = gspca_disconnect,
  246. #ifdef CONFIG_PM
  247. .suspend = gspca_suspend,
  248. .resume = gspca_resume,
  249. #endif
  250. };
  251. /* -- module insert / remove -- */
  252. static int __init sd_mod_init(void)
  253. {
  254. int ret;
  255. ret = usb_register(&sd_driver);
  256. if (ret < 0)
  257. return ret;
  258. PDEBUG(D_PROBE, "registered");
  259. return 0;
  260. }
  261. static void __exit sd_mod_exit(void)
  262. {
  263. usb_deregister(&sd_driver);
  264. PDEBUG(D_PROBE, "deregistered");
  265. }
  266. module_init(sd_mod_init);
  267. module_exit(sd_mod_exit);