finepix.c 7.7 KB

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