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