finepix.c 7.8 KB

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