finepix.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. struct gspca_frame *frame;
  74. int ret = 0;
  75. int len;
  76. /* synchronize with the main driver */
  77. mutex_lock(&gspca_dev->usb_lock);
  78. mutex_unlock(&gspca_dev->usb_lock);
  79. PDEBUG(D_STREAM, "dostream started");
  80. /* loop reading a frame */
  81. again:
  82. while (gspca_dev->present && gspca_dev->streaming) {
  83. /* request a frame */
  84. mutex_lock(&gspca_dev->usb_lock);
  85. ret = command(gspca_dev, 1);
  86. mutex_unlock(&gspca_dev->usb_lock);
  87. if (ret < 0)
  88. break;
  89. if (!gspca_dev->present || !gspca_dev->streaming)
  90. break;
  91. /* the frame comes in parts */
  92. for (;;) {
  93. ret = usb_bulk_msg(gspca_dev->dev,
  94. urb->pipe,
  95. data,
  96. FPIX_MAX_TRANSFER,
  97. &len, FPIX_TIMEOUT);
  98. if (ret < 0) {
  99. /* Most of the time we get a timeout
  100. * error. Just restart. */
  101. goto again;
  102. }
  103. if (!gspca_dev->present || !gspca_dev->streaming)
  104. goto out;
  105. frame = gspca_get_i_frame(&dev->gspca_dev);
  106. if (frame == NULL)
  107. gspca_dev->last_packet_type = DISCARD_PACKET;
  108. if (len < FPIX_MAX_TRANSFER ||
  109. (data[len - 2] == 0xff &&
  110. data[len - 1] == 0xd9)) {
  111. /* If the result is less than what was asked
  112. * for, then it's the end of the
  113. * frame. Sometimes the jpeg is not complete,
  114. * but there's nothing we can do. We also end
  115. * here if the the jpeg ends right at the end
  116. * of the frame. */
  117. if (frame)
  118. frame = gspca_frame_add(gspca_dev,
  119. LAST_PACKET,
  120. frame,
  121. data, len);
  122. break;
  123. }
  124. /* got a partial image */
  125. if (frame)
  126. gspca_frame_add(gspca_dev,
  127. gspca_dev->last_packet_type
  128. == LAST_PACKET
  129. ? FIRST_PACKET : INTER_PACKET,
  130. frame, data, len);
  131. }
  132. /* We must wait before trying reading the next
  133. * frame. If we don't, or if the delay is too short,
  134. * the camera will disconnect. */
  135. msleep(NEXT_FRAME_DELAY);
  136. }
  137. out:
  138. PDEBUG(D_STREAM, "dostream stopped");
  139. }
  140. /* this function is called at probe time */
  141. static int sd_config(struct gspca_dev *gspca_dev,
  142. const struct usb_device_id *id)
  143. {
  144. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  145. struct cam *cam = &gspca_dev->cam;
  146. cam->cam_mode = fpix_mode;
  147. cam->nmodes = 1;
  148. cam->bulk = 1;
  149. cam->bulk_size = FPIX_MAX_TRANSFER;
  150. INIT_WORK(&dev->work_struct, dostream);
  151. return 0;
  152. }
  153. /* this function is called at probe and resume time */
  154. static int sd_init(struct gspca_dev *gspca_dev)
  155. {
  156. return 0;
  157. }
  158. /* start the camera */
  159. static int sd_start(struct gspca_dev *gspca_dev)
  160. {
  161. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  162. int ret, len;
  163. /* Init the device */
  164. ret = command(gspca_dev, 0);
  165. if (ret < 0) {
  166. PDEBUG(D_STREAM, "init failed %d", ret);
  167. return ret;
  168. }
  169. /* Read the result of the command. Ignore the result, for it
  170. * varies with the device. */
  171. ret = usb_bulk_msg(gspca_dev->dev,
  172. gspca_dev->urb[0]->pipe,
  173. gspca_dev->urb[0]->transfer_buffer,
  174. FPIX_MAX_TRANSFER, &len,
  175. FPIX_TIMEOUT);
  176. if (ret < 0) {
  177. PDEBUG(D_STREAM, "usb_bulk_msg failed %d", ret);
  178. return ret;
  179. }
  180. /* Request a frame, but don't read it */
  181. ret = command(gspca_dev, 1);
  182. if (ret < 0) {
  183. PDEBUG(D_STREAM, "frame request failed %d", ret);
  184. return ret;
  185. }
  186. /* Again, reset bulk in endpoint */
  187. usb_clear_halt(gspca_dev->dev, gspca_dev->urb[0]->pipe);
  188. /* Start the workqueue function to do the streaming */
  189. dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
  190. queue_work(dev->work_thread, &dev->work_struct);
  191. return 0;
  192. }
  193. /* called on streamoff with alt==0 and on disconnect */
  194. /* the usb_lock is held at entry - restore on exit */
  195. static void sd_stop0(struct gspca_dev *gspca_dev)
  196. {
  197. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  198. /* wait for the work queue to terminate */
  199. mutex_unlock(&gspca_dev->usb_lock);
  200. destroy_workqueue(dev->work_thread);
  201. mutex_lock(&gspca_dev->usb_lock);
  202. dev->work_thread = NULL;
  203. }
  204. /* Table of supported USB devices */
  205. static const __devinitdata struct usb_device_id device_table[] = {
  206. {USB_DEVICE(0x04cb, 0x0104)},
  207. {USB_DEVICE(0x04cb, 0x0109)},
  208. {USB_DEVICE(0x04cb, 0x010b)},
  209. {USB_DEVICE(0x04cb, 0x010f)},
  210. {USB_DEVICE(0x04cb, 0x0111)},
  211. {USB_DEVICE(0x04cb, 0x0113)},
  212. {USB_DEVICE(0x04cb, 0x0115)},
  213. {USB_DEVICE(0x04cb, 0x0117)},
  214. {USB_DEVICE(0x04cb, 0x0119)},
  215. {USB_DEVICE(0x04cb, 0x011b)},
  216. {USB_DEVICE(0x04cb, 0x011d)},
  217. {USB_DEVICE(0x04cb, 0x0121)},
  218. {USB_DEVICE(0x04cb, 0x0123)},
  219. {USB_DEVICE(0x04cb, 0x0125)},
  220. {USB_DEVICE(0x04cb, 0x0127)},
  221. {USB_DEVICE(0x04cb, 0x0129)},
  222. {USB_DEVICE(0x04cb, 0x012b)},
  223. {USB_DEVICE(0x04cb, 0x012d)},
  224. {USB_DEVICE(0x04cb, 0x012f)},
  225. {USB_DEVICE(0x04cb, 0x0131)},
  226. {USB_DEVICE(0x04cb, 0x013b)},
  227. {USB_DEVICE(0x04cb, 0x013d)},
  228. {USB_DEVICE(0x04cb, 0x013f)},
  229. {}
  230. };
  231. MODULE_DEVICE_TABLE(usb, device_table);
  232. /* sub-driver description */
  233. static const struct sd_desc sd_desc = {
  234. .name = MODULE_NAME,
  235. .config = sd_config,
  236. .init = sd_init,
  237. .start = sd_start,
  238. .stop0 = sd_stop0,
  239. };
  240. /* -- device connect -- */
  241. static int sd_probe(struct usb_interface *intf,
  242. const struct usb_device_id *id)
  243. {
  244. return gspca_dev_probe(intf, id,
  245. &sd_desc,
  246. sizeof(struct usb_fpix),
  247. THIS_MODULE);
  248. }
  249. static struct usb_driver sd_driver = {
  250. .name = MODULE_NAME,
  251. .id_table = device_table,
  252. .probe = sd_probe,
  253. .disconnect = gspca_disconnect,
  254. #ifdef CONFIG_PM
  255. .suspend = gspca_suspend,
  256. .resume = gspca_resume,
  257. #endif
  258. };
  259. /* -- module insert / remove -- */
  260. static int __init sd_mod_init(void)
  261. {
  262. int ret;
  263. ret = usb_register(&sd_driver);
  264. if (ret < 0)
  265. return ret;
  266. PDEBUG(D_PROBE, "registered");
  267. return 0;
  268. }
  269. static void __exit sd_mod_exit(void)
  270. {
  271. usb_deregister(&sd_driver);
  272. PDEBUG(D_PROBE, "deregistered");
  273. }
  274. module_init(sd_mod_init);
  275. module_exit(sd_mod_exit);